Referencing Macro Variables Indirectly

Indirect References

The rules for indirect references for macro variables rules are as follows:
  • When multiple ampersands or percent signs precede a name token, the macro processor resolves two ampersands (&&) to one ampersand (&), and re-scans the reference.
  • To reference macro variables indirectly, the macro processor scans and resolves tokens from left to right from the point where multiple ampersands or percent signs are coded, until no more triggers can be resolved.

Example: Referencing Macro Variables Indirectly

Suppose you want to write a PROC PRINT step that you can reuse without any modification to print information about each course. You can do this by using an indirect reference in the TITLE statement.
data _null_;
   set certadv.courses;
   call symputx(course_code,(course_title));
run;

%let crsid=C002;
proc print data=certadv.schedule noobs label;
   where course_code="&crsid";
   var location begin_date teacher;
   title1 "Schedule for ???";
run;
In the example above, the macro variable C002 (as created by the SYMPUTX routine) has a value of Structured Query Language. Therefore, the TITLE statement should reference a macro variable that resolves to Structured Query Language. Remember that you want this reference to be flexible enough to apply to any of the macro variables that the SYMPUTX routine creates, such as C003 or C004, by changing only the %LET statement.
To obtain the value that you want, you must indirectly reference the macro variable C002 through a reference to the macro variable Crsid. If the value of the macro variable Crsid is C002, the following process might seem to be correct:
  1. Resolve the macro variable Crsid to the value C002.
  2. Attach an ampersand (&) to the front of the resolved value in order to create a new reference (&C002).
  3. Resolve the resulting macro variable reference to the value Structured Query Language.
This sequence seems to imply that you should use the reference &&crsid to convert the value of the macro variable Crsid to the corresponding course description. However, the indirect reference rules indicate that this is not the correct solution.
Here is the correct solution:
title1 "Schedule for &&&crsid";

Example: Creating a Series of Macro Variables

You can create a series of macro variables, Teach1 to Teachn, each containing the name of the instructor who is assigned to a specific course.
options symbolgen;
data _null_;
   set certadv.schedule;
   call symputx(cats('teach',course_number),teacher);
run;
The CATS function converts numeric values to character strings and removes leading and trailing blanks before concatenation. The SYMPUTX call routine eliminates leading and trailing blanks from the macro variables and values.
Then, you can reference one of these variables when a course number is designated. If you designate a course number in a %LET statement, you can use multiple ampersands in order to create a reference to the Teachn macro variable that corresponds to the current course number.
%let crs=3;
proc print data=certadv.register noobs;
   where course_number=&crs;
   var student_name paid;
   title1 "Roster for Course &crs";
   title2 "Taught by &&teach&crs";
run;
The following is written to the SAS log, which shows the steps that lead to the resolution of the reference &&teach&crs.
Log 8.16 SAS Log
SYMBOLGEN:  Macro variable CRS resolves to 3
SYMBOLGEN:  Macro variable CRS resolves to 3
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable CRS resolves to 3
SYMBOLGEN:  Macro variable TEACH3 resolves to Forest, Mr. Peter
Figure 8.1 PROC PRINT Output of Roster for Course 3 Taught by Forest, Mr. Peter
Roster for course 3 Taught by Forest, Mr. Peter
Last updated: October 16, 2019
..................Content has been hidden....................

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