SAS Macro Language: Reference |
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
- 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 Macro Call in the Input Stack shows the lines
remaining
in the input stack after the macro processor compiles the macro definition
APP.
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 (Macro Call Entering Word Queue).
Macro Call Entering Word Queue
The macro processor
recognizes a macro call and begins to execute macro
APP, as follows:
- The macro processor opens the session catalog and creates a local
symbol table with an entry for the parameter GOAL.
- The macro processor removes the tokens for the macro call from
the input stack and places the parameter value in GOAL entry in the APP symbol
table.
- The macro processor encounters the compiled %IF instruction and
recognizes that the next item will be text containing a condition.
- The macro processor places the text
&sysday=Friday
on the
input stack ahead of the remaining text in the program (Text for %IF Condition on Input Stack) and waits for the
word scanner to tokenize the generated text.
Text for %IF Condition on Input Stack
- The word scanner starts
tokenizing the generated text, recognizes
an ampersand followed by nonblank character in the first token, and triggers
the macro processor.
- The macro processor examines the token, 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
(Figure 4.7).
- The macro processor stops and waits for the word scanner to tokenize
the generated text.
Input Stack after Macro Variable Reference Is Resolved
- The word scanner then read
Friday=Friday
from the input stack.
- 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
- The
macro processor executes the compiled %DO instructin and recognizes
that the next item is text.
- The macro processor places the text on top of the input stack
and waits for the word scanner to begin tokenization.
- The word scanner reads the generated text from the input stack,
and tokenizes it.
- 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
- When
the word scanner detects & followed by a nonblank characater
(the macro variable refernece &GOAL), it triggers the macro processor.
- 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 (Figure 4.10).
The Word Scanner Reads Generated Text
- The
word scanner resumes tokenization. When it has completed tokenizing
the generated text, it triggers the macro processor.
- The macro processor resumes processing the compiled macro instructions.
It recognizes the end of the %DO group at the %END instructin and proceeds
to %MEND.
- the macro processor executes the %MEND instruction, removes the
local symbol table APP, and macro APP ceases execution.
- The macro processor triggers the word scanner to resume tokenization.
- The word scanner reads
the first token in input stack (PROC),
recognizes the beginning of a step boundary, and triggers the DATA step compiler.
- The compiled DATA step is executed, and the DATA step compiler
is cleared.
- The word scanner signals the PRINT procedure (a separate executable
not illustrated), which pulls the remaining tokens.
The Remaining Statements are Compiled and Executed
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.