Discover essential SAS macros concepts in this Q&A guide. Learn how to create and identify macro variables, distinguish %LOCAL vs. %GLOBAL scope, reuse code with %INCLUDE and macros, leverage DATA _NULL_, perform arithmetic on macro variables, call macros inside a data step, understand macro variable length limits, and explore SAS validation tools. Perfect for SAS programmers looking to sharpen their macro skills.
Table of Contents
SAS Macros: 10 Key Q&As for Programmers
Describe the way in which one can create a macro variable in SAS?
There are 5 ways to create macro variables in SAS:
%LET statement – defines a macro variable explicitly:
%let var = value;CALL SYMPUT /ÂCALL SYMPUTX – creates a macro variable from a data step:
call symput(‘var’, value);INTO clause inÂPROC SQL – stores query results into macro variables:
select count(*) into :var from dataset;- Macro parameters – defined in a macro definition:
%macro mymacro(var=); - Iterative %Do Statement
How would you identify a macro variable in SAS?
A macro variable is identified in SAS code by an ampersand (&) preceding its name, e.g., &var. To view existing macro variables and their values, one can use:
%PUT _USER_; – lists all user-defined macro variables.%PUT &var; – displays the value of a specific macro variable.
What is the difference between %LOCAL and %GLOBAL in SAS?
%LOCALÂ creates a macro variable with scope limited to the current macro; it is not accessible outside that macro.%GLOBALÂ creates a macro variable accessible anywhere (global scope), even after the macro finishes.
How would you define the end of a macro in SAS?
The end of a macro in SAS is defined using the %MEND statement. Optionally, you can include the macro name after %MEND for clarity (e.g., %MEND mymacro;).
How would you include common code or reuse code to be processed along with your statements?
One can reuse code in SAS by:
- UsingÂ
%INCLUDE to insert an external file containing SAS statements. - Defining macros (
%MACROÂ /Â%MEND) to encapsulate reusable logic, then calling them as needed.
Explain DATA_NULL_
DATA _NULL_; It is a SAS data step that does not create an output dataset. It is used for executing logic without producing data, such as writing to the log, creating macro variables (via CALL SYMPUT), generating custom reports with PUT statements, or performing calculations that only need to be processed in memory.
How do you add a number to a macro variable in SAS?
To add a number to a macro variable, use %EVAL (for integers) or %SYSEVALF (for floating-point) within a %LET statement:
%let var = 5;
%let var = %eval(&var + 3);
For decimal values:
%let var = %sysevalf(&var + 0.5);
How can we call macros within a data step?
One can call a macro within a data step by:
- Directly invoking the macro (e.g.,Â
%my_macro;): it executes during data step compilation, not for each observation. - UsingÂ
CALL EXECUTE: to execute the macro conditionally or for each observation:sascall execute(‘%my_macro’); - UsingÂ
%SYSFUNC: to call a SAS function within a macro without requiring a full macro invocation.
Note: Macro calls placed directly in the data step are resolved before the data step runs, while CALL EXECUTE resolves during data step execution.
What is the maximum length of the macro variable?
The maximum length of a macro variable value in SAS is 65,534 characters. The macro variable name itself can be up to 32 characters long. Note: In very old versions (SAS 6), the maximum length was 32,767 characters. The 65,534 limit applies to SAS System 8 and later.
What validation tools are used in SAS?
The common validation tools in SAS include:
- PROC COMPARE: compares datasets for differences.
- PROC FREQ / PROC MEANS: validates data distributions and summary statistics.
- Data step debugging: usingÂ
PUTÂ statements andÂ_ERROR_Â variable. - Macro debugging options:
MPRINT,ÂSYMBOLGEN,ÂMLOGIC to trace macro resolution. - SAS Code Analyzer (in SAS Enterprise Guide): checks code for errors and best practices.
- DS2 and FedSQL: offer validation features in SAS Viya.
- Validation framework: in SAS Viya for automated model validation.


