The “Forward Re-scan Rule” (FRR) is used by SAS to resolve macro variables over several passes. This is especially useful when having one macro variable point to another macro variable, or when trying to resolve numbered macro variables.
The SAS Advanced Prep Guide summarises the FRR 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 re-scan a reference, 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: numbered list of macro variables
To illustrate this example for a numbered list of macro variables, we can load each unique car manufacturer from SASHELP.CARS into a unique macro variable:
data cars; set sashelp.cars; run; proc sql; select distinct make into :car1-:car999 from cars; quit; %put Number of obs = &sqlobs;
SAS will not create more macro variables than necessary. We have accounted for the possibility of 999 distinct manufacturers, but in reality the dataset contains only 38. SAS will only reserve the variables car1 to car38.
We can loop through the variables we’ve just created, by using the FRR. To further illustrate, also turn on the MPRINT, MLOGIC, and SYMBOLGEN options. Note the use of the double ampersand (&&).
options mprint mlogic symbolgen; %macro printCars; %do i = 1 %to &sqlobs; %put &&car&i; %end; %mend printCars; %printCars;
MLOGIC(PRINTCARS): %DO loop beginning; index variable I; start value is 1; stop value is 38; by value is 1. MLOGIC(PRINTCARS): %PUT &&car&i SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 1 SYMBOLGEN: Macro variable CAR1 resolves to Acura
From the log output, we can trace the FRR resolution as follows:
Example: nested macro variables
Suppose we declare the following macro variables:
%let one = two; %let two = three; %let three = one;
To test your understanding of the FRR, can you accurately predict the resolution of these macro variables?
%put &one; %put &&one; %put &&&one; %put &&&&one; %put &&&&&one; %put &&&&&&one; %put &&&&&&&one; %put &&&&&&&&one; %put &&&&&&&&&one; %put &&&&&&&&&&one;
The FRR will process from left to right. Any double ampersand (&&) will be resolved to a single ampersand (&) and any instances of a single ampersand will be resolved.
Let’s work through two examples together.
To better organise our desk-checking of the code, we can rewrite it in a more human-readable, and -friendly format:
&& && &one;
Each double ampersand (&&) resolves to a single ampersand, and the remaining single ampersand and macro reference is resolved.
& & two;
The remaining two ampersand are resolved to a single ampersand:
Which resolves to:
And upon checking the SAS log, we can see our result has been confirmed:
%put &&&&&one; three
As a final example, let’s work through:
Organise the ampersands into a more human-readable format, group and resolve double ampersands, and resolve remaining single ampersands:
&&&&&&&&one; && && && && one; & & & & one; compress ==> && && one; & & one; compress ==> && one; &one; two
Our result is once again confirmed by the SAS log:
%put &&&&&&&&one; two