SAS Nested Macro Variables & Multi-Ampersand Resolution Rules
Master indirect referencing (&var, &&var, &&&var), left-to-right scanning passes, and dynamic loop variable resolution.
Nested Macro Variables and multi-ampersand references (such as &&var&i or &&&name) enable indirect referencing in the SAS Macro Facility, where the value of one macro variable supplies the name of another macro variable. The SAS Macro Processor resolves consecutive ampersands mechanically using three rules: scanning strictly from left to right, reducing every pair of consecutive ampersands (&&) into a single literal ampersand (&), and repeating the resolution scan iteratively until zero resolvable ampersands remain.
Why This Concept Matters in Real-World Practice #
Dynamic macro automation in clinical batch runs and enterprise data engineering relies on building variable names on the fly inside %DO loops (e.g. referencing &lab1 through &lab5 via &&lab&i). Without understanding multi-pass scanning, programmers struggle with unresolved tokens, unexpected syntax halts, and invalid symbol table lookups.
Standard Syntax, Derivation & Framework Pattern #
/* ========================================================= */
/* 1. CLASSIC 4-TIER "FUNNY BOOK" RESOLUTION TRACE */
/* ========================================================= */
%let book = story;
%let story = funny;
%let funny = twist;
%let twist = climax;
%put 1 Ampersand: &book; /* -> story */
%put 2 Ampersands: &&book; /* -> story */
%put 3 Ampersands: &&&book; /* -> funny */
%put 4 Ampersands: &&&&book; /* -> story */
%put 7 Ampersands: &&&&&&&book; /* -> twist */
%put 15 Ampersands: &&&&&&&&&&&&&&&book; /* -> climax */
/* ========================================================= */
/* 2. PRODUCTION WORKHORSE: DYNAMIC LOOP ITERATION (&&var&i) */
/* ========================================================= */
%let lab1 = Systolic_BP;
%let lab2 = Diastolic_BP;
%let lab3 = Heart_Rate;
%macro summarize_vitals;
%do i = 1 %to 3;
/* Pass 1: && -> & | lab -> lab | &i -> 1 ===> &lab1
Pass 2: &lab1 -> Systolic_BP */
%put Iteration &i Parameter: &&lab&i;
%end;
%mend summarize_vitals;
%summarize_vitals;
Core Rules #
- Rule 1 (Left-to-Right Scan): The Macro Processor parses tokens strictly from left to right across the expression.
- Rule 2 (Double-Ampersand Reduction): Every pair of consecutive ampersands (&&) collapses into a single literal ampersand (&) in the current pass.
- Rule 3 (Recursive Re-Scanning): If any ampersands remain after a pass, the processor rescans the transformed token until all references are resolved.
- Dynamic Array Formula: In iterative loops, &&var&i always resolves in two passes: Pass 1 transforms && to & and resolves &i to 1 (yielding &var1); Pass 2 resolves &var1 to its stored value.
- Auditing Best Practice: Enable OPTIONS SYMBOLGEN MPRINT; to display exact scanner pass transformations in the SAS log.
Step-by-step scanner traces, the 4-tier funny book chain, and dynamic loop automation without headache.
Frequently Asked Questions (FAQ) #
Why does &&var&i resolve to &var1 in Pass 1 instead of evaluating &var first?
Because Rule 1 dictates left-to-right scanning and Rule 2 converts && to a single & literal without evaluating 'var'. Simultaneously, &i has a single ampersand, so it resolves to its current loop integer (e.g. 1), producing &var1 for Pass 2.
What happens if I write &var&i instead of &&var&i?
SAS attempts to resolve &var immediately during Pass 1. Since no macro variable named 'var' exists in the symbol table, SAS issues a WARNING: Apparent symbolic reference VAR not resolved and outputs the literal text 'var1'.
How do I see exactly how SAS resolves my multiple ampersands?
Submit OPTIONS SYMBOLGEN; in your SAS session. The SAS log will then display explicit notes detailing each resolution step (e.g., SYMBOLGEN: && resolves to &, SYMBOLGEN: Macro variable I resolves to 1, SYMBOLGEN: Macro variable VAR1 resolves to Systolic_BP).
Want complete video breakdowns, code files & exercises?
Explore comprehensive step-by-step masterclass training in our authoritative SAS Certification Course & Programming Training or test your code mechanics on the SAS Base Exam Simulator (325 Questions).