May 2006 Technical Tip SAS' Forward Rescan Rule

Within SAS, a single ampersand is used to return the value of a macro variable. For example, given the SAS statement:

%let month = January;

The value of month can be retrieved using &month as in the following:

%put The month is &month;

But what if the value of the macro is itself the name of another macro? This situation requires understanding of the Forward Rescan Rule. Explaining that rule is the purpose of this article.

What follows is a concise and complete example of:

  1. how to create macro variables within a DATA step using CALL SYMPUT,
  2. how to reference a macro variable within a literal, and
  3. how to reference macro variables created in a DATA step within a subsequent PROC when the macro variable itself is the name of another macro variable.

  data kids;
	input name $ age;
	call symput(name, age);
  datalines;
  Cora 21
  Emma 15
  Hannah 17
  William 23
  ;

  %let who = Hannah;

  proc print data=kids;
  title "Kids older than &who";
  where age > &&&who;
  run;
Download source here.

First, let's look at the call symput statement. The call symput statement in the DATA step creates a macro variable for each observation: the name of each variable is the person's name (for example Cora, not name) and the value of that macro variable is that person's age (for example, 21.) So in this example, four separate macro variables are created in the DATA step. Note that macro variables created within a DATA step as shown here cannot be referenced in that same DATA step.

Next, let's look at the title statement. This is a trivial example. There is only one ampersand so the macro variable &who resolves to Hannah. The reader is reminded that literals containing macro variables must be enclosed in quotes, not apostrophes.

Finally, let's look closely at the where statement. When multiple ampersands precede a name token, the macro processor resolves two ampersands to a single ampersand and then re-scans. This is known as the Forward Rescan Rule. So where age > &&&who resolves to where age > &hannah which itself resolves to where age > 17. The observations for Cora (age 21) and William (age 23) are printed.

We hope you will consider Caliber Data Training when you are in need of high quality SAS training.


Go to the articles index. Written by Bill Qualls. Copyright © 2006 by Caliber Data Training 800.938.1222