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:
The value of month can be retrieved using &month as in the following:
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:
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.