Lesson 9, Part 9: Non-macro vs. Macro Program Flow¶
Macros/macro variables are resolved before the compilation phase (i.e., before the PDV is created)
In [4]:
from IPython.display import Image
Image("C:/Data/Macro_Diagram_x.png")
#Source: https://www.analyticsvidhya.com/blog/2015/12/complete-tutorial-sas-macros-faster-data-manipulation/
Out[4]:
- The
SAScode is submitted. - SAS statements are held in the
Input Stack. - The
Word Scannerparses the SAS statements intotokens - Tokens are sent to the
Data Step Compilerfor syntax checking and execution. - If the
Word Scannerfindsmacro triggers(& or %), it sends them toMacro Processor. - In
Macro Processormacro statements are executed andmacro variable referencesare resolved. - The output from the
macro processoris sent back to theInput Stack/Word Scanner. - If the
Word Scannerdoes not detect any more macro language elements, the SAS code is sent to theData Step Compiler.
The tokens can be:
Keywords (DATA, SET, IF, etc.)
Names (like dataset or variable names)
Constants (like numbers or strings)
Special characters - Any character that is not a letter, number, or underscore, such as the following:
= + − % & ; ( )
Macro triggers (which are also special characters)
* % indicates a macro keyword or macro variable reference (like %let, %macro, or %put)
* & triggers a macro variable resolution (like &varname)
So when the word scanner encounters % or &, it knows: “Hey! Time to pass this to the macro processor, not just treat it like regular SAS code!” (ChatGPT)
In [ ]:
data work.example;
set sashelp.class;
if age > 12 then output;
run;
The word scanner turns the above code snippets into tokens during compilation phase.
[DATA] [work.example] [;] [SET] [sashelp.class] [;] [IF] [age] [>] [12] [THEN] [OUTPUT] [;] [RUN] [;]
In [1]:
from IPython.display import Image
Image("C:/Data/Macro_Diagram3.png")
# Source: https://codeblab.com/wp-content/uploads/2010/06/SAS-macros.pdf
# Author:JAY A. JAFFE. SAS® MACROS BEYOND THE BASICS
Out[1]:
In [3]:
from IPython.display import Image
Image("C:/Data/Macro_Diagram1.png")
#Source: Carpenter, Art. 2016. Carpenter's Guide to the SAS Macro Language
# Obtained from https://stackoverflow.com/questions/37321659/sas-macro-flow-explanation
Out[3]:
In [1]:
from IPython.display import Image
Image("C:/Data/Macro_Diagram2.png")
#Source: Lepp, Tim. 2019. SAS Macro Quoting - A Look Behind the Scenes.
# The Clinical Data Science Conference. EU 2019.
Out[1]:
- Literal: One or more characters enclosed in single or double quotation marks.
'CARY'
"2008"
- Name: One or more characters beginning with a letter or an underscore. Other characters can be letters, underscores, and digits.
data _test linesleft f25
univariate otherwise year_2008 descending
- Number: A numeric value Integers or Real (floating point) numbers.
1, 72, and 5000
'24AUG2008'D
2.35, 5., 2.3E1, and 5.4E− 1
- Special character. Any character that is not a letter, number, or underscore
= + − % & ; ( ) #
In [2]:
from IPython.display import Image
Image("C:/Data/Macro_Quoting.png")
# Source: As above.
Out[2]:
MACRO PROCESSOR – The Masked Warrior
%macro sortit;
proc sort data=&syslast;
by study subject gender;
run;
%mend sortit;
%sortit;
In [1]:
from IPython.display import Image
Image("C:/Data/Macro_Compilation.png")
# Source: As above.
Out[1]:
In [2]:
from IPython.display import Image
Image("C:/Data/Macro_Execution.png")
# Source: As above.
Out[2]:
In [ ]: