Previous Page | Next Page

SAS Programs and Macro Processing

How SAS Processes Statements with Macro Activity

In a program with macro activity, the macro processor can generate text that is placed on the input stack to be tokenized by the word scanner. The example in this section shows you how the macro processor creates and resolves a macro variable. To illustrate how the compiler and the macro processor work together, the following figure contains the macro processor and the macro variable symbol table. SAS creates the symbol table at the beginning of a SAS session to hold the values of automatic and global macro variables. SAS creates automatic macro variables at the beginning of a SAS session. For the sake of illustration, the symbol table is shown with only one automatic macro variable, SYSDAY.

The Macro Processor and Symbol Table

[The Macro Processor and Symbol Table]

Whenever the word scanner encounters a macro trigger, it sends information to the macro processor. A macro trigger is either an ampersand (&) or percent sign (%) followed by a nonblank character. As it did in the previous example, the word scanner begins to process this program by examining the first characters in the input stack. In this case, the word scanner finds a percent sign (%) followed by a nonblank character. The word scanner recognizes this combination of characters as a potential macro language element, and triggers the macro processor to examine % and LET, as shown in the following figure.

The Macro Processor Examines LET

[The Macro Processor Examines LET]

When the macro processor recognizes a 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 in the following figure.

The Macro Processor Writes to the Symbol Table

[The Macro Processor Writes to the Symbol Table]

From the time the word scanner triggers the macro processor until that macro processor action is complete, the macro processor controls all activity. While the macro processor is active, no activity occurs in the word scanner or the DATA step compiler.

When the macro processor is finished, the word scanner reads the next token (the DATA keyword in this example) and sends it to the compiler. The word scanner triggers the compiler, which begins to pull tokens from the top of the queue, as shown in the following figure.

The Word Scanner Resumes Tokenization

[The Word Scanner Resumes Tokenization]

As it processes each token, SAS removes the protection that the macro quoting functions provide to mask special characters and mnemonic operators. For more information, see Macro Quoting.

If the word scanner finds an ampersand followed by a nonblank character in a token, it triggers the macro processor to examine the next token, as shown in the following figure.

The Macro Processor Examines &FILE

[The Macro Processor Examines macro variable named FILE]

The macro processor examines the token and recognizes a macro variable that exists in the symbol table. The macro processor removes the macro variable name from the input stack and replaces it with the text from the symbol table, as shown in the following figure.

The Macro Processor Generates Text to the Input Stack

[The Macro Processor Generates Text to the Input Stack]

The compiler continues to request tokens, and the word scanner continues to supply them, until the entire input stack has been read as shown in the following figure.

The Word Scanner Completes Processing

[The Word Scanner Completes Processing]

If the end of the input stack is a DATA step boundary, as it is in this example, the compiler compiles and executes the step. SAS then frees the DATA step task. Any macro variables that were created during the program remain in the symbol table. If the end of the input stack is not a step boundary, the processed statements remain in the compiler. Processing resumes when more statements are submitted to the input stack.

Previous Page | Next Page | Top of Page