After a macro variable is created, you typically use the variable
by referencing it with an ampersand preceding its name (&
variable-name), which is called a
macro variable reference. These references perform
symbolic substitutions when they resolve to their value. You can use
these references anywhere in a SAS program. To resolve a macro variable
reference that occurs within a literal string, enclose the string
in double quotation marks. Macro variable references that are enclosed
in single quotation marks are not resolved. Compare the following
statements that assign a value to macro variable DSN and use it in
a TITLE statement:
%let dsn=Newdata;
title1 "Contents of Data Set &dsn";
title2 'Contents of Data Set &dsn';
In the first TITLE statement,
the macro processor resolves the reference by replacing &DSN with
the value of macro variable DSN. In the second TITLE statement, the
value for DSN does not replace &DSN. SAS sees the following statements:
TITLE1 "Contents of Data Set Newdata";
TITLE2 'Contents of Data Set &dsn';
You can refer to a macro
variable as many times as you need to in a SAS program. The value
remains constant until you change it. For example, this program refers
to macro variable DSN twice:
%let dsn=Newdata;
data temp;
set &dsn;
if age>=20;
run;
proc print;
title "Subset of Data Set &dsn";
run;
Each time the reference
&DSN appears, the macro processor replaces it with
Newdata
. SAS sees the following statements:
DATA TEMP;
SET NEWDATA;
IF AGE>=20;
RUN;
PROC PRINT;
TITLE "Subset of Data Set NewData";
RUN;
Note: If you reference a macro
variable that does not exist, a warning message is printed in the
SAS log. For example, if macro variable JERRY is misspelled as JERY,
the following produces an unexpected result:
%let jerry=student;
data temp;
x="produced by &jery";
run;
This code produces the following message:
WARNING: Apparent symbolic reference JERY not resolved.