Home › Forums › Main Forums › SAS Forum › Alert: Be cautious when use Call Symput!
-
Alert: Be cautious when use Call Symput!
-
Given the below codes:
Data _null_ ;
set AAA End=EOF NOBS=N;
call symput("ZZZ", N);
file "C:\Users\Desktop\SessionE\Test.txt";
put _all_;
if EOF then do;
put "this is the end of the text file, which has &ZZZ records.";
end;
run;You will find: when you execute the codes for the FIRST time, the macro variable ZZZ created by Call Symput is NOT resolved. You will get a warning and below result in LOG:
WARNING: Apparent symbolic reference ZZZ not resolved.
“this is the end of the text file, which has &ZZZ records.”However, if you do nothing and execute the codes again, then this time ZZZ is resolved, and the external file has this at its end:
“this is the end of the text file, which has 180 records.”
Strange? Unbelievable? How come?
Answer:
This is because Call Symput creates macro variable at execution phase, while the direct reference to &ZZZ precedes at compile phase, therefore, is can not be resolved at all.
When we execute the codes again, the macro variable ZZZ has already been created from previous execution, and kept in memory, therefore it is resolved without any problem.
Conclusion:
We need be very cautious when referring to macro variables created by Call Symput, they lag behind the direct reference ” &Var ” and could not be resolved at all.
-
From SAS Support Website:
Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available.
One of the most common problems in using SYMPUT is trying to reference a macro variable value assigned by SYMPUT before that variable is created.
The failure generally occurs because the statement referencing the macro variable compiles before execution of the CALL SYMPUT statement that assigns the variable’s value. The most important fact to remember in using SYMPUT is that it assigns the value of the macro variable during program execution, but macro variable references resolve during the compilation of a step, a global statement used outside a step, or an SCL program. As a result:
* You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value.
* You must specify a step boundary statement to force the DATA step to execute before referencing a value in a global statement following the program (for example, a TITLE statement). The boundary could be a RUN statement or another DATA or PROC statement. For example:
data x;
X='December';
call symput('var', X);
Run;
proc print;
title "Report for &var";
run;
Log in to reply.