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
Overview of CALL SYMPUT Routine
Most problems with CALL SYMPUT involve the lack of a precise step boundary
between the CALL SYMPUT statement that creates the macro variable and the macro
variable reference that uses that variable. (For more information, see “CALL SYMPUT
Routine” on page 240.) However, a few special cases exist that involve the scope of a
macro variable created by CALL SYMPUT. These cases are good examples of why you
should always assign a scope to a variable before assigning a value rather than relying on
SAS to do it for you.
Two rules control where CALL SYMPUT creates its variables:
1. CALL SYMPUT creates the macro variable in the current symbol table available
while the DATA step is executing, provided that symbol table is not empty. If it is
empty (contains no local macro variables), usually CALL SYMPUT creates the
variable in the closest nonempty symbol table.
2. However, there are three cases where CALL SYMPUT creates the variable in the
local symbol table, even if that symbol table is empty:
Special Cases of Scope with the CALL SYMPUT Routine 65
Beginning with SAS Version 8, if CALL SYMPUT is used after a PROC SQL,
the variable will be created in a local symbol table.
If the macro variable SYSPBUFF is created at macro invocation time, the
variable will be created in the local symbol table.
If the executing macro contains a computed %GOTO statement, the variable will
be created in the local symbol table. A computed %GOTO statement is one that
uses a label that contains an & or a % in it. That is, a computed %GOTO
statement contains a macro variable reference or a macro call that produces a text
expression. Here is an example of a computed %GOTO statement:
%goto &home;
The symbol table that is currently available to a DATA step is the one that exists when
SAS determines that the step is complete. (SAS considers a DATA step to be complete
when it encounters a RUN statement, a semicolon after data lines, or the beginning of
another step.)
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 if the local symbol table was not empty, and creates a local
macro variable.
You might 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.
Example Using CALL SYMPUT with Complete DATA Step and a
Nonempty Local Symbol Table
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:
WARNING: Apparent symbolic reference MYVAR1 not resolved.
This message appears for the following reasons:
the DATA step is complete within the environment of ENV1 (that is, the RUN
statement is within the macro)
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.
66 Chapter 5 Scopes of Macro Variables
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 following figure shows all of the symbol tables in this example.
Special Cases of Scope with the CALL SYMPUT Routine 67
Figure 5.9 The Symbol Tables with the CALL SYMPUT Routine Generating a Complete DATA Step
Example Using CALL SYMPUT with an Incomplete 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";
68 Chapter 5 Scopes of Macro Variables
run;
These statements execute without errors. The DATA step is complete only when SAS
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 following figure shows all the scopes in this example.
Special Cases of Scope with the CALL SYMPUT Routine 69
..................Content has been hidden....................

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