Wednesday 14 December 2016

Macro Processing

In this post, i am going to explain how the macro's are compiled and executed in SAS.

When a piece of code is selected and executed, that piece of code is moved to "Input Stack". Once the piece of code is in Input Stack, SAS transforms the code into Individual tokens. This process of converting the code into tokens is called "Tokenization" and it is done by "Word Scanner". Here, the Word Scanner starts from the first character of Input Stack and assembles the them into different types of tokens.


There are 4 types of tokens:

1. Literal :- A string of characters enclosed in quotation marks.

2. Number :- Digits, Date Values, Time Values, Hexadecimal Numbers.

3. Name :- A string of characters beginning with an underscore or a letter.

4. Special :- Any character or group of character which has special meaning to SAS. For ex., * / +  - ** ; $ () , % = etc.

Below is the code used for explanation.


The first SAS statement has eight tokens. When the Word Scanner finds a blank or the beginning of the next token, it removes the token and transfers to the bottom of the queue. In this example, when the word scanner pulls the first token, it recognizes the token as the beginning of a new data step and triggers the Data Step Compiler. The Data Step Compiler now start requesting for the tokens, and Word Scanner pulls tokens from the top and provides it to the Compiler.



The compiler continues to request the tokens until word scanner recognizes the end of Data Step.


How SAS processes statements with macro activity??



SAS creates symbol table at the beginning of the session to hold the values of automatic and global macro variables. SAS creates the automatic macro variables at the beginning of the SAS session. Here, for the sake of example, we have shown only SYSDAY variable.


Whenever the word scanner encounters a macro trigger, it sends the information to macro processor. A macro trigger is either & or % followed by non blank character. The word scanner begins to process the program by examining the first character of input stack. In this case, the word scanner finds a % sign followed by a non blank character and triggers the macro processor. 


When a macro processor recognizes the macro language element, it begins to work with the word scanner. In this case, the macro processor removes the % LET statement and writes an entry in the symbol table as shown below.



From the time word scanner triggers the macro processor until the macro processor action is complete, the macro processor controls the activity. While macro processor is active, no action occurs in word scanner or Data Step Compiler.


When Macro processor is finished, the word scanner reads the next token (DATA) and sends it to Data Step Compiler, which begins to pull the tokens from the top of the queue.
As it processes each token, the word scanner finds an & followed by non blank character in a token and it triggers the macro processor to examine the next token.

The macro processor examines the next token and recognizes a macro variable that exists in the symbol table. The macro processor removes the macro variable name in the input stack and replaces it with the value of the macro variable.

Once the macro processor completes its action, Data Step Compilers continues to request for the tokens until all the tokens are read. If the end of the word scanner is a data step boundary, the compiler executes the step.

No comments:

Post a Comment