You can reference macro
variables anywhere in your program. Some applications might require
placing a macro variable reference adjacent to leading text (text&variable) or trailing text (&variabletext)
or referencing adjacent macro variables (&variable&variable)
in order to build a new token. When you combine macro variable references
and text, it is important to keep in mind how SAS interprets tokens.
Remember that a token
ends when the word scanner detects either the beginning of a new token
or a blank after a token.
You can place text immediately
before a macro variable reference to build a new token. For example,
suppose that data sets are stored in a SAS library, using the naming
convention
Yyymon,
where
yy is a two-digit year
such as 02 or 01, and
mon is
a three-letter month such as JUN or AUG. Data set names could include
examples such as Y01DEC and Y02MAR. You can write a program that uses
a macro variable to build the month portion of the SAS data set name.
%let month=jan;
proc chart data=sasuser.y02&month;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y02&month;
plot sale*day;
run;
Table 9.14 Code after Substitution
proc chart data=sasuser.y02jan;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y02jan;
plot sale*day;
run;
|
You can reference macro
variables that have no blanks between them to build new tokens.
For example, you can
modify the previous program to enable both the month and the year
to be substituted:
%let year=02;
%let month=jan;
proc chart data=sasuser.y&year&month;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y&year&month;
plot sale*day;
run;
Table 9.15 Code after Substitution
proc chart data=sasuser.y02jan;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y02jan;
plot sale*day;
run;
|
The generated program
is identical to the program in the previous example. That is, the
compiler sees the same code for both of these examples.
You can place text immediately
after a macro variable reference as long as the macro variable name
can still be tokenized correctly.
For example, you can
modify the previous program to substitute the name of an analysis
variable:
%let year=02;
%let month=jan;
%let var=sale;
proc chart data=sasuser.y&year&month;
hbar week / sumvar=&var;
run;
proc plot data=sasuser.y&year&month;
plot &var*day;
run;
Table 9.16 Code after Substitution
proc chart data=sasuser.y02jan;
hbar week / sumvar=sale;
run;
proc plot data=sasuser.y02jan;
plot sale*day
run;
|
The generated
program is identical to the program in the previous two examples.
That is, although you are changing the code that you submit, you are
not changing the code that the compiler sees.