Chapter 5
Scopes of Macro Variables
Scopes of Macro Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Global Macro Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Local Macro Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Writing the Contents of Symbol Tables to the SAS Log . . . . . . . . . . . . . . . . . . . . . . 53
How Macro Variables Are Assigned and Resolved . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Examples of Macro Variable Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Changing the Values of Existing Macro Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Creating Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Forcing a Macro Variable to Be Local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Creating Global Macro Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Creating Global Variables Based on the Value of Local Variables . . . . . . . . . . . . . . 65
Special Cases of Scope with the CALL SYMPUT Routine . . . . . . . . . . . . . . . . . . . . 65
Overview of CALL SYMPUT Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Example Using CALL SYMPUT with Complete DATA Step
and a Nonempty Local Symbol Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Example Using CALL SYMPUT with an Incomplete DATA Step . . . . . . . . . . . . . 68
Example Using CALL SYMPUT with a Complete DATA
Step and an Empty Local Symbol Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Example Using CALL SYMPUT with SYSPBUFF and an
Empty Local Symbol Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Scopes of Macro Variables
Every macro variable has a scope. A macro variable's scope determines how it is
assigned values and how the macro processor resolves references to it.
Two types of scopes exist for macro variables: global and local. Global macro variables
exist for the duration of the SAS session and can be referenced anywhere (except
CARDS and DATALINES) in the program—either inside or outside a macro. Local
macro variables exist only during the execution of the macro in which the variables are
created and have no meaning outside the defining macro.
Scopes can be nested, like boxes within boxes. For example, suppose you have a macro
A that creates the macro variable LOC1 and a macro B that creates the macro variable
LOC2. If the macro B is nested (executed) within the macro A, LOC1 is local to both A
and B. However, LOC2 is local only to B.
49
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset