A macro that calls another macro that calls a third one is normal in a clinical programming library. When the output is wrong, the log is often no help: either it shows nothing about the macro logic, or, with every debug option on, thousands of lines.
A reliable order: turn on the right options, look at the generated code instead of the macro code, and check variable scope first, because that is where most nested macro bugs come from.
Step 1: the three options, and when each helps
options mprint mlogic symbolgen;
- MPRINT shows the SAS code the macro generated. Start with this one. Most bugs are visible in the generated code.
- MLOGIC shows the macro's own logic: which
%ifwas true, loop counters, parameter values on entry. - SYMBOLGEN shows every macro variable as it resolves. Useful, but very noisy. Switch it on only around the part you are checking.
For nested calls, add MPRINTNEST and MLOGICNEST. They prefix each log line with the full call chain (for example OUTER.INNER.HELPER), so you can tell which level produced it.
options mprint mprintnest mlogic mlogicnest;
Step 2: save the generated code to a file
Reading generated code in the log is tiring. With MFILE, SAS writes it to a file you can open, read and even run on its own:
filename mprint "/tmp/generated_code.sas";
options mprint mfile;
%derive_adsl(indata=sdtm.dm);
options nomfile;
Now you are debugging plain SAS code, which is much easier than reasoning about the macro that wrote it.
Step 3: check scope before anything else
This is the most common bug in nested macros. Both macros use a loop counter called i, and the inner one doesn't declare it local:
%macro inner;
%do i = 1 %to 2; /* no %local i */
%put inner i=&i;
%end;
%mend inner;
%macro outer;
%local i;
%do i = 1 %to 3;
%inner
%put outer i=&i;
%end;
%mend outer;
%outer
When %inner runs, it looks for an existing i, finds the outer macro's one, and changes it. After the first call the outer counter is 3 (the inner loop ends at 3), so the outer loop runs once instead of three times. There is no error and no warning.
The fix is one line in the inner macro:
%macro inner;
%local i;
%do i = 1 %to 2;
%put inner i=&i;
%end;
%mend inner;
A good team rule: every macro declares every variable it creates with %LOCAL, unless it is meant to return a value through a global.
Step 4: print exactly what you need
Instead of SYMBOLGEN for the whole run, print specific values at the point you care about:
%put &=indata &=byvars; /* prints INDATA=sdtm.dm BYVARS=usubjid */
%put _local_; /* all local variables of the current macro */
%put _user_; /* all user-defined macro variables */
%put &=var writes the name and the value together, which saves a lot of guessing when several values look alike.
Other common causes
- Single quotes. Macro variables don't resolve inside single quotes.
'&study'stays as literal text. Use double quotes. - A missing semicolon after a macro call that generates a full step can join two statements together. MPRINT shows it straight away.
- Values with commas passed as parameters split into extra arguments. Quote them with
%str()or%bquote().
Write macros that are easier to debug next time
The cheapest debugging is the kind you never need. Two habits help: check parameters at the top and stop with a clear message, and give the macro a debug switch so nobody has to edit it to see what it does. The same pattern in R uses stopifnot() and a debug argument:
%macro derive_flag(indata=, outdata=, var=, debug=N);
%local rc;
%if %length(&indata) = 0 or %length(&var) = 0 %then %do;
%put ERROR: DERIVE_FLAG: INDATA= and VAR= are required.;
%return;
%end;
%if not %sysfunc(exist(&indata)) %then %do;
%put ERROR: DERIVE_FLAG: dataset &indata does not exist.;
%return;
%end;
%if %upcase(&debug) = Y %then %do;
options mprint mlogic symbolgen;
%end;
data &outdata;
set &indata;
length &var.fl $1;
if not missing(&var) then &var.fl = 'Y';
run;
%if %upcase(&debug) = Y %then %do;
options nomlogic nosymbolgen;
%end;
%mend derive_flag;
%derive_flag(indata=adae, outdata=adae2, var=aeser, debug=Y)
derive_flag <- function(data, var, debug = FALSE) {
stopifnot(is.data.frame(data), var %in% names(data))
if (debug) message("derive_flag: ", nrow(data), " rows in, flagging ", var)
data[[paste0(var, "FL")]] <- ifelse(!is.na(data[[var]]), "Y", NA_character_)
if (debug) message("derive_flag: ", sum(!is.na(data[[var]])), " records flagged")
data
}
adae2 <- derive_flag(adae, "AESER", debug = TRUE)
Note &var.fl in the SAS version: the period ends the macro variable name, so aeser plus fl becomes aeserfl. Without the period SAS would look for a macro variable called varfl.
In R, traceback() after an error shows which function in a chain failed, and browser() placed inside a function pauses it so you can inspect values. These do the job of MLOGIC and %PUT in an interactive session.
Try it yourself: why doesn't the title resolve?
%let study = CSP001;
title 'Summary of Adverse Events, Study &study';
The output title shows &study literally. Why, and how do you fix it?
Answer: the macro processor does not look inside single-quoted strings in SAS statements, so &study is never resolved. Use double quotes: title "Summary of Adverse Events, Study &study";. If the text itself must contain a single quote, double quotes still work: "Subject's &study data".
Quick check
1. Which option shows the SAS code that a macro generated?
MPRINT shows generated code. MLOGIC shows macro logic, and SYMBOLGEN shows macro variable resolution.
2. An inner macro uses %do i = 1 %to 2; without %local i;. The outer macro has %local i; and loops with it. What happens?
Without %LOCAL, the inner macro finds and updates the existing i in the calling macro's scope.
3. What does %put &=dsn; print when DSN is adsl?
The &= form writes the variable name and its value together.
- Start with MPRINT, add MPRINTNEST for nested calls, and use MFILE to debug the generated code.
- Check
%localdeclarations first. Shared loop counters cause most nested macro bugs. - Use
%put &=varinstead of SYMBOLGEN for targeted checks. - Build parameter checks and a debug switch into every macro.
Related: Seeing %INCLUDE code in the log · More SAS articles