Scopes of Macro Variables |
Local macro variables are defined within an individual macro. Each macro you invoke creates its own local symbol table. Local macro variables exist only as long as a particular macro executes. When the macro stops executing, all local macro variables for that macro cease to exist.
The following figure illustrates the local symbol table during the execution of the following program.
%macro holinfo(day,date); %let holiday=Christmas; %put *** Inside macro: ***; %put *** &holiday occurs on &day, &date, 2002. ***; %mend holinfo; %holinfo(Wednesday,12/25) %put *** Outside macro: ***; %put *** &holiday occurs on &day, &date, 2002. ***;
The %PUT statements write the following to the SAS log:
*** Inside macro: *** *** Christmas occurs on Wednesday, 12/25, 2002. *** *** Outside macro: *** WARNING: Apparent symbolic reference HOLIDAY not resolved. WARNING: Apparent symbolic reference DAY not resolved. WARNING: Apparent symbolic reference DATE not resolved. *** &holiday occurs on &day, &date, 2002. ***
As you can see from the log, the local macro variables DAY, DATE, and HOLIDAY resolve inside the macro, but outside the macro they do not exist and therefore do not resolve.
A macro's local symbol table is empty until the macro creates at least one macro variable. A local symbol table can be created by any of the following:
the presence of one or more macro parameters
a %LOCAL statement
macro statements that define macro variables, such as %LET and the iterative %DO statement (if the variable does not already exist globally or a %GLOBAL statement is not used)
Note: Macro parameters are always local to the macro that defines them. You cannot make macro parameters global. (Although, you can assign the value of the parameter to a global variable. See Creating Global Variables Based on the Value of Local Variables.)
When you invoke one macro inside another, you create nested scopes. Because you can have any number of levels of nested macros, your programs can contain any number of levels of nested scopes.
You can use the %SYMLOCAL function to indicate whether an existing macro variable resides in an enclosing local symbol table. See the %SYMLOCAL Function for more detailed information.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.