Chapter Contents |
Previous |
Next |
SAS Macro Language: Reference |
Changing the Values of Existing Macro Variables |
When the macro processor executes a macro program statement that can create a macro variable (such as a %LET statement), the macro processor attempts to change the value of an existing macro variable rather than create a new macro variable. The %GLOBAL and %LOCAL statements are exceptions.
To illustrate, consider the following %LET statements. Both statements assign values to the macro variable NEW:
%let new=inventry; %macro name1; %let new=report; %mend name1;
Suppose you submit the following statements:
%name1 data &new;
These statements produce the following statement:
data report;
Because NEW exists as a global variable, the macro processor changes the value of that variable rather than creating a new one. The macro NAME1's local symbol table remains empty.
Figure 5.5 illustrates the contents of the global and local symbol tables before, during, and after NAME1's execution.
Snapshots of Symbol Tables
Creating Local Variables |
When the macro processor executes a macro program statement that can create a macro variable, the macro processor creates the variable in the local symbol table if no macro variable with the same name is available to it. Consider the following example:
%let new=inventry; %macro name2; %let new=report; %let old=warehse; %mend name2; %name2 data &new; set &old; run;
After NAME2 executes, the SAS compiler sees the following statements:
data report; set &old; run;
The macro processor encounters the reference &OLD after macro NAME2 has finished executing; thus, the macro variable OLD no longer exists. The macro processor is not able to resolve the reference and issues a warning message.
Symbol Tables at Various Stages illustrates the contents of the global and local symbol tables at various stages.
Symbol Tables at Various Stages
But suppose you place the SAS statements inside the macro NAME2, as in the following program:
%let new=inventry; %macro name2; %let new=report; %let old=warehse; data &new; set &old; run; %mend name2; %name2
In this case, the macro processor generates the SET statement during the execution of NAME2, and it locates OLD in NAME2's local symbol table. Therefore, executing the macro produces the following statements:
data report; set warehse; run;
The same rule applies regardless of how many levels of nesting exist. Consider the following example:
%let new=inventry; %macro conditn; %let old=sales; %let cond=cases>0; %mend conditn; %macro name3; %let new=report; %let old=warehse; %conditn data &new; set &old; if &cond; run; %mend name3; %name3
The macro processor generates these statements:
data report; set sales; if &cond; run;
CONDITN finishes executing before the macro processor reaches the reference &COND, so no variable named COND exists when the macro processor attempts to resolve the reference. Thus, the macro processor issues a warning message and generates the unresolved reference as part of the constant text and issues a warning message. Symbol Tables Showing Two Levels of Nesting shows the symbol tables at each step.
Symbol Tables Showing Two Levels of Nesting
Notice that the placement of a macro invocation is what creates a nested scope, not the placement of the macro definition. For example, invoking CONDITN from within NAME3 creates the nested scope; it is not necessary to define CONDITN within NAME3.
Forcing a Macro Variable to Be Local |
At times you need to ensure that the macro processor creates a local macro variable rather than changing the value of an existing macro variable. In this case, use the %LOCAL statement to create the macro variable.
Explicitly make all macro variables created within macros local when you do not need their values after the macro stops executing. Debugging the large macro programs is easier if you minimize the possibility of inadvertently changing a macro variable's value. Also, local macro variables do not exist after their defining macro finishes executing, while global variables exist for the duration of the SAS session; therefore, local variables use less overall storage.
Suppose you want to use the macro NAMELST to create a list of names for a VAR statement, as shown here:
%macro namelst(name,number); %do n=1 %to &number; &name&n %end; %mend namelst;
You invoke NAMELST in this program:
%let n=North State Industries; proc print; var %namelst(dept,5); title "Quarterly Report for &n"; run;
After macro execution, the SAS compiler sees the following statements:
proc print; var dept1 dept2 dept3 dept4 dept5; title "Quarterly Report for 6"; run;
The macro processor changes the value of the global variable N each time it executes the iterative %DO loop. (After the loop stops executing, the value of N is 6, as described in " %DO" in Chapter 13, "Macro Language Dictionary.") To prevent conflicts, use a %LOCAL statement to create a local variable N, as shown here:
%macro namels2(name,number); %local n; %do n=1 %to &number; &name&n %end; %mend namels2;
Now execute the same program:
%let n=North State Industries; proc print; var %namels2(dept,5); title "Quarterly Report for &n"; run;
The macro processor generates the following statements:
proc print; var dept1 dept2 dept3 dept4 dept5; title "Quarterly Report for North State Industries"; run;
Global and Local Variables with the Same Name shows the symbol tables before NAMELS2 executes, while NAMELS2 is executing, and when the macro processor encounters the reference &N in the TITLE statement.
Global and Local Variables with the Same Name
Creating Global Macro Variables |
The %GLOBAL statement creates a global macro variable if a variable with the same name does not already exist there, regardless of what scope is current.
For example, in the macro NAME4, the macro CONDITN contains a %GLOBAL statement that creates the macro variable COND as a global variable:
%macro conditn; %global cond; %let old=sales; %let cond=cases>0; %mend conditn;
Here is the rest of the program:
%let new=inventry; %macro name4; %let new=report; %let old=warehse; %conditn data &new; set &old; if &cond; run; %mend name4; %name4
Invoking NAME4 generates these statements:
data report; set sales; if cases>0; run;
Suppose you want to put the SAS DATA step statements outside NAME4. In this case, all the macro variables must be global for the macro processor to resolve the references. You cannot add OLD to the %GLOBAL statement in CONDITN because the %LET statement in NAME4 has already created OLD as a local variable to NAME4 by the time CONDITN begins to execute. (You cannot use the %GLOBAL statement to make an existing local variable global.)
Thus, to make OLD global, use the %GLOBAL statement before the variable reference appears anywhere else, as shown here in the macro NAME5:
%let new=inventry; %macro conditn; %global cond; %let old=sales; %let cond=cases>0; %mend conditn; %macro name5; %global old; %let new=report; %let old=warehse; %conditn %mend name5; %name5 data &new; set &old; if &cond; run;
Now the %LET statement in NAME5 changes the value of the existing global variable OLD rather than creating OLD as a local variable. The SAS compiler sees the following statements:
data report; set sales; if cases>0; run;
Creating Global Variables Based on the Value of Local Variables |
To use a local variable such as a parameter outside a macro, use a %LET statement to assign the value to a global variable with a different name, as in this program:
%macro namels3(name,number); %local n; %global g_number; %let g_number=&number; %do n=1 %to &number; &name&n %end; %mend namels3;
Now invoke the macro NAMELS3 in the following the program:
%let n=North State Industries; proc print; var %namels3(dept,5); title "Quarterly Report for &n"; footnote "Survey of &g_number Departments"; run;
The compiler sees the following statements:
proc print; var dept1 dept2 dept3 dept4 dept5; title "Quarterly Report for North State Industries"; footnote "Survey of 5 Departments"; run;
Special Cases of Scope with the CALL SYMPUT Routine |
Most problems with CALL SYMPUT involve the lack of an explicit step boundary between the CALL SYMPUT statement that creates the macro variable and the macro variable reference that uses that variable. (See Chapter 8, "Interfaces with the Macro Facility," for details on CALL SYMPUT.) However, a few special cases exist that involve the scope of a macro variable created by CALL SYMPUT.
Two rules control where CALL SYMPUT creates its variables:
%goto &home;
The symbol table that is currently available to a DATA step is the one that exists when the SAS System determines that the step is complete. (The SAS System considers a DATA step to be complete when it encounters a RUN statement, a semicolon after data lines, or the beginning of another step).
In simplest terms, if an executing macro contains a computed %GOTO statement, or if the macro variable SYSPBUFF is created at macro invocation time, but the local symbol table is empty, CALL SYMPUT behaves as though the local symbol table was not empty, and creates a local macro variable.
You may find it helpful to use the %PUT statement with the _USER_ option
to determine what symbol table the CALL SYMPUT routine has created the variable
in.
Consider the following example, which contains a complete DATA step with a CALL SYMPUT statement inside a macro:
%macro env1(param1); data _null_; x = 'a token'; call symput('myvar1',x); run; %mend env1; %env1(10) data temp; y = "&myvar1"; run;
When you submit these statements, you receive an error message:
WARNNG: Apparent symbolic reference MYVAR1 not resolved.
This message appears because the DATA step is complete within the environment of ENV1 (that is, the RUN statement is within the macro) and because the local symbol table of ENV1 is not empty (it contains parameter PARAM1). Therefore, the CALL SYMPUT routine creates MYVAR1 as a local variable for ENV1, and the value is not available to the subsequent DATA step, which expects a global macro variable.
To see the scopes, add a %PUT statement with the _USER_ option to the macro, and a similar statement in open code. Now invoke the macro as before:
%macro env1(param1); data _null_; x = 'a token'; call symput('myvar1',x); run; %put ** Inside the macro: **; %put _user_; %mend env1; %env1(10) %put ** In open code: **; %put _user_; data temp; y = "&myvar1"; /* ERROR - MYVAR1 is not available in open code. */ run;
When the %PUT _USER_ statements execute, they write the following information to the SAS log:
** Inside the macro: ** ENV1 MYVAR1 a token ENV1 PARAM1 10 ** In open code: **
The MYVAR1 macro variable is created by CALL SYMPUT in the local ENV1 symbol table. The %PUT _USER_ statement in open code writes nothing to the SAS log, because no global macro variables are created.
The CALL SYMPUT Routine in a Macro Generating a Complete DATA Step shows all of the symbol tables in this example.
The CALL SYMPUT Routine in a Macro Generating a Complete DATA Step
In the macro ENV2, shown here, the DATA step is not complete within the macro because there is no RUN statement:
%macro env2(param2); data _null_; x = 'a token'; call symput('myvar2',x); %mend env2; %env2(20) run; data temp; y="&myvar2"; run;
These statements execute without errors. The DATA step is complete only when the SAS System encounters the RUN statement (in this case, in open code); thus, the current scope of the DATA step is the global scope. CALL SYMPUT creates MYVAR2 as a global macro variable, and the value is available to the subsequent DATA step.
Again, use the %PUT statement with the _USER_ option to illustrate the scopes:
%macro env2(param2); data _null_; x = 'a token'; call symput('myvar2',x); %put ** Inside the macro: **; %put _user_; %mend env2; %env2(20) run; %put ** In open code: **; %put _user_; data temp; y="&myvar2"; run;
When the %PUT _USER_ statement within ENV2 executes, it writes the following to the SAS log:
** Inside the macro: ** ENV2 PARAM2 20
The %PUT _USER_ statement in open code writes the following to the SAS log:
** In open code: ** GLOBAL MYVAR2 a token
The CALL SYMPUT Routine in a Macro Generating an Incomplete DATA Step shows all the scopes in this example.
The CALL SYMPUT Routine in a Macro Generating an Incomplete DATA Step
In the following example, ENV3 does not use macro parameters. Therefore, its local symbol table is empty:
%macro env3; data _null_; x = 'a token'; call symput('myvar3',x); run; %put ** Inside the macro: **; %put _user_; %mend env3; %env3 %put ** In open code: **; %put _user_; data temp; y="&myvar3"; run;
In this case, the DATA step is complete and executes within the macro, but the local symbol table is empty. So, CALL SYMPUT creates MYVAR3 in the closest available nonempty symbol table--the global symbol table. Both %PUT statements show that MYVAR3 exists in the global symbol table:
** Inside the macro: ** GLOBAL MYVAR3 a token ** In open code: ** GLOBAL MYVAR3 a token
In the following example, the presence of the SYSPBUFF automatic macro variable causes CALL SYMPUT to behave as though the local symbol table were not empty, even though the macro ENV4 has no parameters or local macro variables:
%macro env4 /parmbuff; data _null_; x = 'a token'; call symput('myvar4',x); run; %put ** Inside the macro: **; %put _user_; %put &syspbuff; %mend env4; %env4 %put ** In open code: **; %put _user_; %put &syspbuff; data temp; y="&myvar4"; /* ERROR - MYVAR4 is not available in open code */ run;
The presence of the /PARMBUFF specification causes the SYSPBUFF automatic macro variable to be created. So, when you call macro ENV4, CALL SYMPUT creates the macro variable MYVAR4 in the local symbol table (that is, in ENV4's), even though the macro ENV4 has no parameters and no local variables.
The results of the %PUT statements prove this--the score of MYVAR4 is listed as ENV4, and the reference to SYSPBUFF does not resolve in the open code %PUT statement because SYSPBUFF is local to ENV4:
** Inside the macro: ** ENV4 MYVAR4 a token ** In open code: ** WARNING: Apparent symbolic reference SYSPBUFF not resolved.
For more information about SYSPBUFF, see Chapter 13.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.