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]:
No description has been provided for this image
  • The SAS code is submitted.
  • SAS statements are held in the Input Stack.
  • The Word Scanner parses the SAS statements into tokens
  • Tokens are sent to the Data Step Compiler for syntax checking and execution.
  • If the Word Scanner finds macro triggers (& or %), it sends them to Macro Processor.
  • In Macro Processor macro statements are executed and macro variable references are resolved.
  • The output from the macro processor is sent back to the Input Stack/Word Scanner.
  • If the Word Scanner does not detect any more macro language elements, the SAS code is sent to the Data 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] [;]

How SAS Processes Statements without Macro Activity

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]:
No description has been provided for this image
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]:
No description has been provided for this image
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]:
No description has been provided for this image

List of SAS Tokens

  • 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
= + − % & ; ( ) #

Macro Quoting: Which Function Should We Use?

In [2]:
from IPython.display import Image
Image("C:/Data/Macro_Quoting.png")
# Source: As above.
Out[2]:
No description has been provided for this image

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]:
No description has been provided for this image
In [2]:
from IPython.display import Image
Image("C:/Data/Macro_Execution.png")
# Source: As above.
Out[2]:
No description has been provided for this image
In [ ]: