SAS and the Forward Re-scan Rule

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:

  1. &&car&i
  2. &car1
  3. Acura

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.

%put &&&&&one;

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:

&two;

Which resolves to:

three

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:

%put &&&&&&&&one;

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

SAS LOCF For Multiple Variables

It is often necessary to replace missing measurements with the closest, previous measurement. This technique is referred to as LOCF (last observation carried forward).

In this example, we will create a dataset with 4 columns: subject ID, visit number, body weight, and systolic blood pressure.

data have;
	input ID VISIT WT SBP;
	cards;
1 10 85 125
1 20 84 .
1 30 86 .
1 40 . 130
1 50 . 128
1 60 85 .
2 10 . 110
2 20 90 .
2 30 91 .
2 40 91 123
2 50 . .
2 60 . 130
;
run;
Input dataset, with subject ID, visit number, body weight, and systolic blood pressure

Thereafter, we will sort the dataset to ensure it is in the order we expect. Never assume that your input will be appropriately sorted.

proc sort data=have;
	by id visit;
run;

Now we will define 2 macro variables, which are simply lists of variables. The first contains the original variables available in the dataset, which will not be altered, and the second names the variables which will contain the LOCF values.

%let origvars = %str(WT  SBP);
%let locfvars = %str(WT_ SBP_);

This brings us to our final block of code. The lists of variables defined above are loaded into arrays and a loop performs the LOCF operation across all the variables defined.

data want(drop = j);
	set have;
	by id visit;

	/*Create arrays of the variable lists*/
	array orig[*] &origvars.;
	array locf[*] &locfvars.;
	retain 	      &locfvars.;

	do j = 1 to dim(orig);
		if first.id then do;
			/*Set a placeholder value for initial missings*/
			if orig(j) = . then locf(j) = -99;
		end;

		/*Replace retained value with latest non-missing value*/
		if not missing(orig(j)) then locf(j) = orig(j);
	end;
run;
The final dataset with LOCF’ed variables, WT_ and SBP_