Combining Macro Variable References with Text

Overview

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.

Delimiters in Macro Variable Names

Sometimes you might want to place a macro variable name immediately before text other than a special character. For example, you might want to modify the previous program so that it is easy to switch between using the CHART and PLOT procedures of Base SAS software and the GCHART and GPLOT procedures of SAS/GRAPH software.
/* GRAPHICS should be null or G */
%let graphics=g;
%let year=02;
%let month=jan;
%let var=sale;
proc &graphicschart data=sasuser.y&year&month;
   hbar week / sumvar=&var;
run;
proc &graphicsplot data=sasuser.y&year&month;
   plot &var*day;
run;
The messages written to the SAS log reveal problems with this program.
Table 9.17 SAS Log
13     %let graphics=g;
14     %let year=02;
15     %let month=jan;
16     %let var=sale;
17     proc &graphicschart data=sasuser.y&year&month;
            -
            10

WARNING: Apparent symbolic reference GRAPHICSCHART not resolved.

ERROR 10-205: Expecting the name of the procedure to be executed.
SAS interprets the macro variable's name to be graphicschart instead of graphics because there is no delimiter between the macro variable reference and the trailing text.
The word scanner recognizes the end of a macro variable name when it encounters a special character that cannot be part of the name token. In other words, the special character acts as a delimiter. For example, a period (.) is a special character that is treated as part of the macro variable reference and that does not appear when the macro variable is resolved.
To correct the problem in the previous example, you need to add a period after the reference to the macro variable graphics.
 %let graphics=g;
 %let year=02;
 %let month=jan;
 %let var=sale;
 proc &graphics.chart data=sasuser.y&year&month;
   hbar week / sumvar=&var;
run;
proc &graphics.plot data=sasuser.y&year&month;
   plot &var*day;
run;
When these SAS statements are executed
  • the word scanner treats &graphics. as the reference
  • the value of the macro variable graphics is returned to the input stack
  • the word scanner processes gchart as one token.
Table 9.18 Code after Substitution
proc gchart data=sasuser.y02jan;
   hbar week / sumvar=sale;
run;
proc gplot data=sasuser.y02jan;
   plot sale*day;
run;
We can extend this example and further modify the previous program to include a macro variable that is used to define the libref:
%let lib=sasuser;
%let graphics=g;
%let year=02;
%let month=jan;
%let var=sale;
libname &lib 'SAS-data-library';
proc &graphics.chart data=&lib.y&year&month;
   hbar week / sumvar=&var;
run;
proc &graphics.plot data=&lib.y&year&month;
   plot &var*day;
run;
Notice, however, that this code does not perform the desired substitutions.
Table 9.19 Code after Substitution
libname sasuser 'SAS-data-library';
proc gchart data=sasusery02jan;
   hbar week / sumvar=sale;
run;
proc gplot data=sasusery02jan;
   plot sale*day;
run;
The period after &lib is interpreted as a delimiter. You need to use a second period after the delimiter period to supply the necessary token:
%let lib=sasuser;
...
libname &lib 'SAS-data-library';
proc &graphics.chart data=&lib..y&year&month;
...
proc &graphics.plot data=&lib..y&hear&month;
The first period is treated as a delimiter, and the second period is treated as text.
Table 9.20 Code after Substitution
proc gchart data=sasuser.y02jan;
...
proc gplot data=sasuser.y02jan;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.159