Executive Summary: The Macro Scanner Engine

Staring at &&var&i or &&&param in production SAS code often looks like punctuation soup. But the SAS Macro Processor is not guessing—it evaluates ampersands with 100% mathematical determinism. The key is understanding that in the Macro World, everything is unquoted plain text. Macro evaluation executes strictly in pre-compilation passes using three mechanical rules: (1) Scan left-to-right, (2) Collapse every pair of double ampersands (&&) into a single literal ampersand (&), and (3) Recursively rescan until no ampersands remain. Once you master this scanning rhythm, building dynamic multi-table loops, clinical macro arrays, and self-generating pipelines becomes second nature.

Figure 1: The Three Inflexible Rules of Multi-Level Ampersand Resolution
Classroom Blueprint
SAS Macro Ampersand Resolution Rules and Funny Book Trace
Figure 1: The foundational scanner architecture: (1) Left-to-right evaluation, (2) Pair reduction (&& → &), and (3) Recursive re-scanning across nested tiers until zero ampersands remain.

The Two Realities of SAS: Data World vs. Macro World #

To master nested macros, you must first separate SAS into its two distinct operating realms:

  • The Data World: This is where DATA steps and PROC steps execute. In the Data World, variables have strict numeric or character datatypes, memory is allocated via the Program Data Vector (PDV), and code is compiled into binary instructions before processing observations one by one.
  • The Macro World: This is a pre-compilation text-generation factory. The Macro Processor does not know what a dataset, a row, or a floating-point number is. To the Macro Processor, everything is plain text strings. If you write %let x = 3 + 5;, the value of &x is not the number 8; it is literally the five-character text string "3 + 5".

When you reference a macro variable with an ampersand (&var), the Macro Processor intercepts the code before the compiler ever sees it, fetches the text from the symbol table, and substitutes the characters directly into your code stream. When macro variables contain the names of other macro variables, we enter the realm of indirect referencing.

The Three Inflexible Rules of Ampersand Resolution #

Whenever the SAS word scanner encounters consecutive ampersands, it adheres to three mechanical rules without exception:

RULE 1 Left-to-Right Scan: The Macro Processor reads the token string sequentially starting from the first character on the left and moving toward the right.
RULE 2 Double Ampersand Reduction (&& → &): Every pair of consecutive ampersands collapses into exactly one literal ampersand in the output stream for that pass. It does not evaluate the variable name behind it yet.
RULE 3 Recursive Re-Scanning: If any ampersands remain in the token after a scanning pass, the processor starts over from left to right, repeating Rules 1 and 2 until no resolvable ampersands exist.

The Classroom "Funny Book" Trace #

In our Made2Stick SAS classroom videos, we use a 4-tier chain of macro variables to prove that no matter how complex the nesting looks, the machine never makes a mistake:

/* 4-Tier Nested Definitions */
%let book  = story;
%let story = funny;
%let funny = twist;
%let twist = climax;
Figure 2: Nested Macro Variables as Encapsulated Clouds
Classroom Blueprint
SAS Nested Macro Variables Illustrated as Clouds: book, story, funny, twist, climax
Figure 2: Visualizing multi-level nesting: the outer red cloud (book → story) encapsulates the blue cloud (funny), which encloses the green cloud (twist), which finally surrounds the innermost core (climax).

Let's trace what happens when we increase the number of ampersands:

Input Token Pass 1 Scan Pass 2 Scan Final Value
&book Resolves &book → story — story
&&book && collapses to &; book remains book → yields &book Resolves &book → story story
&&&book && → &; &book → story → yields &story Resolves &story → funny funny
&&&&book && → &; && → &; book → book → yields &&book &&book → &book (Pass 3 → story) story
&&&&&&&book 3 pairs of && (3 &) + &book (story) → yields &&&story &&&story → &funny (Pass 3 → twist) twist

The Extreme Test: 15 Ampersands #

What happens if you write 15 consecutive ampersands: %put &&&&&&&&&&&&&&&book;?

  • Pass 1: The 15 ampersands break down into 7 consecutive pairs (&&) plus one trailing single ampersand before the word book. The 7 pairs collapse into 7 single ampersands. The trailing &book resolves to story. Output token: &&&&&&&story.
  • Pass 2: 7 ampersands break down into 3 pairs (3 &) plus &story (which resolves to funny). Output token: &&&funny.
  • Pass 3: 3 ampersands break down into 1 pair (1 &) plus &funny (which resolves to twist). Output token: &twist.
  • Pass 4: The single ampersand &twist resolves to its symbol table value: climax.
Figure 3: Authentic SAS Studio Execution Log Live Output
SAS Studio Log Output verifying ampersand resolution to climax
Figure 3: SAS Studio execution log verifying the exact step-down progression from 1 through 15 ampersands ending in climax.

The Production Workhorse: Dynamic Loop Iteration (&&var&i) #

In real-world data engineering, nobody writes 15 ampersands. But you will write &&var&i almost every single week.

Suppose you have a list of clinical endpoints or financial accounts stored in indexed macro variables:

%let var1 = Heart_Rate;
%let var2 = Systolic_BP;
%let var3 = Diastolic_BP;

%macro generate_stats;
    %do i = 1 %to 3;
        proc means data=clinical_vitals mean std min max;
            var &&var&i;
            title "Summary Statistics for &&var&i";
        run;
    %end;
%mend generate_stats;

Why &var&i Fails (The Trainee Trap) #

If you write &var&i with only one ampersand before var, the Macro Processor scans left-to-right (Rule 1). It immediately tries to resolve &var. Because there is no macro variable named var in the symbol table, SAS writes a nasty warning in your log:

WARNING: Apparent symbolic reference VAR not resolved.

It then leaves var as literal text and resolves &i to 1, sending the invalid column name var1 to the procedure step.

Why &&var&i Works Flawlessly #

With two ampersands:

  1. Pass 1: && collapses into a single & (Rule 2). The word var has no ampersand, so it stays var. Then &i has a single ampersand, so it resolves to its current loop number 1. Output of Pass 1: &var1.
  2. Pass 2: Because an ampersand remains, the scanner rescans (Rule 3). &var1 resolves to its stored value: Heart_Rate.

Viewing Resolutions Cleanly with %PUT and SYMBOLGEN #

When building nested automation in complex environments, don't guess what the scanner is doing. Turn on SAS's built-in auditing system:

options symbolgen mprint;

/* In the SAS Log, you will see the exact multi-pass trace: */
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable VAR1 resolves to Heart_Rate

Combining OPTIONS SYMBOLGEN; with formatted %PUT statements (such as %put ------> &&var&i;) lets you inspect token resolution without polluting your production dataset log.

Authoritative Reference Knowledge Hub Guide

SAS Nested Macro Variables & Ampersand Resolution Guide #

Explore our encyclopedic Knowledge Hub guide with GEO direct answers, scanner pass rules, interactive FAQs, and structured JSON-LD educational schemas.

Read Knowledge Hub Guide →
Related Masterclass Lesson (Section 6)

Lesson 6.03.02: More on Ampersand Resolutions #

Watch Aslam Khan demonstrate the funny book 15-ampersand resolution and live SAS Studio log execution in our interactive video masterclass player.