In the previous example, you learned you had to use
CALL SYMPUT to conditionally assign a macro variable a value in a
DATA step. So, you submit the following program:
%let sr_age = 0;
data senior;
set census;
if age > 65 then
do;
call symput("sr_age",age);
put "This data set contains data about a person";
put "who is &sr_age years old."; /* ERROR */
end;
run;
If AGE was 67, you would
expect to see a log message like the following:
This data set contains data about a person
who is 67 years old.
However, no matter what
AGE is, the following message is sent to the log:
This data set contains data about a person
who is 0 years old.
When the DATA step is
being compiled, &SR_AGE is sent to the macro facility for resolution,
and the result is passed back before the DATA step executes. To achieve
the desired result, submit this corrected program instead:
%let sr_age = 0;
data senior;
set census;
if age > 65 then
do;
call symput("sr_age",age);
stop;
end;
run;
data _null_;
put "This data set contains data about a person";
put "who is &sr_age years old.";
run;
Note: Use double quotation marks
in statements like PUT, because macro variables do not resolve when
enclosed in single quotation marks.
Here is another example
of erroneously referring to a macro variable in the same step that
creates it:
data _null_;
retain total 0;
set mydata end=final;
total=total+price;
call symput("macvar",put(total,dollar14.2));
if final then put "*** total=&macvar ***"; /* ERROR */
run;
When these statements
are submitted, the following lines are written to the SAS log:
WARNING: Apparent symbolic reference MACVAR not resolved.
*** total=&macvar ***
As this DATA step is
tokenized and compiled, the
&
causes
the word scanner to trigger the macro processor, which looks for a
MACVAR entry in a symbol table. Because such an entry does not exist,
the macro processor generates the warning message. Because the tokens
remain on the input stack, they are transferred to the DATA step compiler.
During DATA step execution, the CALL SYMPUT statement creates the
macro variable MACVAR and assigns a value to it. However, the text
&macvar
in the PUT statement occurs
because the text has already been processed while the macro was being
compiled. If you were to resubmit these statements and the macro would
appear to work correctly, but the value of MACVAR would reflect the
value set during the previous execution of the DATA step. This value
can be misleading.
Remember that in general,
the
%
and
&
trigger immediate execution or resolution during the compilation
stage of the rest of your SAS code.