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

Get SAS 9.4 Macro Language, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.