Figure 4.3 The Compiled Macro APP
How the Macro Processor Executes a Compiled
Macro
Macro execution begins with the macro processor opening the SASMacr catalog to read
the appropriate macro entry. As the macro processor executes the compiled instructions
in the macro entry, it performs a series of simple repetitive actions. During macro
execution, the macro processor does the following:
executes compiled macro program instructions
places noncompiled constant text on the input stack
waits for the word scanner to process the generated text
resumes executing compiled macro program instructions
To continue the example from the previous section, the following figure shows the lines
remaining in the input stack after the macro processor compiles the macro definition
APP.
Figure 4.4 The Macro Call in the Input Stack
The word scanner examines the input stack and detects % followed by a nonblank
character in the first token. It triggers the macro processor to examine the token.
42 Chapter 4 Macro Processing
Figure 4.5 Macro Call Entering Word Queue
The macro processor recognizes a macro call and begins to execute macro APP, as
follows:
1. The macro processor creates a local symbol table for the macro. The macro
processor examines the previously compiled definition of the macro. If there are any
parameters, variable declarations, or computed GOTO statements in the macro
definition, the macro processor adds entries for the parameters and variables to the
newly created local symbol table.
2. The macro processor further examines the previously compiled macro definition for
parameters to the macro. If no parameters were defined in the macro definition, the
macro processor begins to execute the compiled instructions of the macro. If any
parameters were contained in the definition, the macro processor removes tokens
from the input stack to obtain values for positional parameters and non-default
values for keyword parameters. The values for parameters found in the input stack
are placed in the appropriate entry in the local symbol table.
Note: Before executing any compiled instructions, the macro processor removes
only enough tokens from the input stack to ensure that any tokens that are
supplied by the user and pertain to the macro call have been removed.
3. The macro processor encounters the compiled %IF instruction and recognizes that
the next item will be text containing a condition.
4. The macro processor places the text &sysday=Friday on the input stack ahead of
the remaining text in the program. (See the following figure). The macro processor
waits for the word scanner to tokenize the generated text.
How the Macro Processor Executes a Compiled Macro 43
Figure 4.6 Text for %IF Condition on Input Stack
1. The word scanner starts tokenizing the generated text, recognizes an ampersand
followed by nonblank character in the first token, and triggers the macro processor.
2. The macro processor examines the token and finds a possible macro variable
reference, &SYSDAY. The macro processor first searches the local APP symbol
table for a matching entry and then the global symbol table. When the macro
processor finds the entry in the global symbol table, it replaces macro variable in the
input stack with the value Friday. (See the following figure.)
3. The macro processor stops and waits for the word scanner to tokenize the generated
text.
44 Chapter 4 Macro Processing
Figure 4.7 Input Stack After Macro Variable Reference Is Resolved
1. The word scanner then read Friday=Friday from the input stack.
2. The macro processor evaluates the expression Friday=Friday and, because the
expression is true, proceeds to the %THEN and %DO instructions.
Figure 4.8 Macro Processor Receives the Condition
1. The macro processor executes the compiled %DO instructions and recognizes that
the next item is text.
How the Macro Processor Executes a Compiled Macro 45
2. The macro processor places the text on top of the input stack and waits for the word
scanner to begin tokenization.
3. The word scanner reads the generated text from the input stack, and tokenizes it.
4. The word scanner recognizes the beginning of a DATA step, and triggers the
compiler to begin accepting tokens. The word scanner transfers tokens to the
compiler from the top of the stack.
Figure 4.9 Generated Text on Top of Input Stack
1. When the word scanner detects & followed by a nonblank character (the macro
variable reference &GOAL), it triggers the macro processor.
2. The macro processor looks in the local APP symbol table and resolves the macro
variable reference &GOAL to 10000. The macro processor places the value on top
of the input stack, ahead of the remaining text in the program.
46 Chapter 4 Macro Processing

Get SAS 9.4 Macro Language, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.