HOW SAS PROCESSES
STATEMENTS
Advanced SAS
Adapted from JE Blum, UNCW and the SAS 9.3 Macro Language Reference
An Example
• Here is code that will produce summary statistics.
%LET DSN=sasuser.diabetes;
%LET CLASS=SEX;
%LET variables=age height weight pulse;
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
quit;
Macro Variables
• The Macro variables DSN, Class, and Variables
store information that will be used to make code
“dynamic”.
• All macro variables are character (special functions
will be required to do numerical operations).
• Macro variables follow the same naming
conventions as other variables; however, they are
always preceded by an ampersand (&) when
referenced.
What is the Macro Facility Doing?
• When the SAS program runs, the macro language’s
primary function is as a text substitution facility and/or
code generator.
• To fully understand how macro processing works, we
have to look at how SAS processes submitted code.
Program Processing and Flow
• A SAS program can be any combination of:
• Global statements
• Data steps and PROC steps
• Structured Query Language (SQL)
• SAS Component Language (SCL)
• SAS macro language
• All submitted programs, regardless of content, are
delivered to the input stack.
Compilation and Execution
• When the code is delivered to the input stack,
SAS…
• Reads text in the input stack
• Routes text to the appropriate compiler
• Suspends activity when a step boundary is reached.
• Executes compiled code if there are no errors
• Repeats as necessary.
The Word Scanner--Tokenization
• The word scanner breaks up the text into units
known as tokens.
• The process (open code):
• The word scanner passes tokens to the compiler, and the
compiler requests tokens until it receives a semicolon.
• The compiler performs a syntax check on the statement.
Tokens
• There are four types of tokens:
• Literals: Any quoted string
“Any text string”
‘Any text string’
• Numbers: Strings of digits. May include decimal points and
scientific notation
23 23.7
.7
23.7E-10
• Names: Strings of characters beginning with a letter or
underscore
libname
_n_
mmddyy10.
• Special: Special characters
$
( )
.
&
%
fred
Macro Processing
Compiler
Macro Processor
Word Scanner
%LET
Input Stack
DSN=sasuser.diabetes;
%LET CLASS=SEX;
%LET variables=age height weight pulse;
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
quit;
Macro Processing
Compiler
Macro Processor
%LET
Word Scanner
%LET
Input Stack
DSN=sasuser.diabetes;
%LET CLASS=SEX;
%LET variables=age height weight pulse;
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
The % acts as a macro
trigger. The % and the
following name token
are sent to the macro
processor.
Macro Processing
Compiler
Macro Processor
%LET
Word Scanner
DSN
=
Sasuser
.
Diabetes
;
Input Stack
DSN=sasuser.diabetes;
%LET CLASS=SEX;
%LET variables=age height weight pulse;
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
The word scanner
will continue to
deliver tokens to
the macro processor
until a semicolon is
reached
Macro Processing
Compiler
Macro Processor
Word Scanner
Symbol Table
Input Stack
%LET CLASS=SEX;
%LET variables=age height weight pulse;
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
The %let statement
assigns a value to the
macro variable
Process continues with other two %LET Statements
Compiler
Macro Processor
Word Scanner
Symbol Table
Input Stack
Title 'Summary Statistics';
proc means data=&DSN maxdec=2;
class &CLASS;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Tokens process up to first macro variable
Compiler
Title 'Summary Statistics';
proc means data=
Macro Processor
Word Scanner
&DSN
Symbol Table
Input Stack
maxdec=2;
class &CLASS;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Value of &DSN found in Symbol table
Compiler
Title 'Summary Statistics';
proc means data=
Macro Processor
&DSN
Word Scanner
Symbol Table
Input Stack
maxdec=2;
class &CLASS;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Value of &DSN found in Symbol table
Compiler
Title 'Summary Statistics';
proc means data=SASUSER.DIABETES;
Macro Processor
&DSN
Word Scanner
Symbol Table
Input Stack
maxdec=2;
class &CLASS;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Substitutions continue for other tokens and macro variables
Compiler
Title 'Summary Statistics';
proc means data=SASUSER.DIABETES
maxdec=2;
class
Macro Processor
Word Scanner
&CLASS
Symbol Table
Input Stack
;
var &variables;
run;
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Until compiler has complete program with substitutions made.
Compiler
Title 'Summary Statistics';
proc means data=SASUSER.DIABETES
maxdec=2;
class;
var age height weight pulse;
run;
Word Scanner
Input Stack
Macro Processor
Symbol Table
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
End of boundary found (RUN;) so program executes…
Compiler
Title 'Summary Statistics';
proc means data=SASUSER.DIABETES
maxdec=2;
class;
var age height weight pulse;
run;
Word Scanner
Input Stack
Macro Processor
Symbol Table
Variable
Value
DSN
SASUSER.DIABETES
CLASS
SEX
VARIABLES
age height
weight pulse
Program now runs…
Automatic Macro Variables
• By default, SAS has several macro variables established
on its own. These variables
• are created at the invocation of the SAS session
• are global (always available)
• some have been assigned values by SAS (some are null)
• values can be re-assigned by users in some cases
Some Automatic Macro Variables
Name
Value
SYSDATE
Date of SAS invocation (DATE7.)
SYSDATE9 Date of SAS invocation (DATE9.)
SYSDAY
Day of the week of SAS invocation
SYSTIME
Time of SAS invocation
SYSVER
Version of SAS being used
SYSLAST
Name of the most recently created data set. If none
have been created, value is _NULL_.
Viewing Macro Variables
• The macro language has its own “put” statement.
• Syntax: %put text;
• Writes a new line to the SAS Log
• No quotes are required around text
• Resolves any macro triggers in text
• Can be used in open code
Viewing Automatic Macro Variables
• The statement:
%put _automatic_;
Writes names and values of all automatic macro variables to the Log.
Put that statement in the code, run, and look at LOG…
How SAS Compiles a Macro
• Similar to previous example – reads from Input Stack into
Word Scanner  creates compiled macro in catalog
• See pages 34ff in SAS Macro reference
• Suppose this code is in the Input Stack
%macro simple(dsn=,variable=);
proc means data=&DSN;
var &variable;
run;
%mend simple;
%simple(dsn="C:\sasdata\somedata",variable=time1-time4);
Similar to previous example, Input Stack is read into Word Scanner. It is then
processed and put into a Macro Catalog
Macro Catalog
Macro Processor
Word Scanner
Symbol Table
Input Stack
%macro simple(dsn=,variable=);
proc means data=&DSN;
var &variable;
run;
%mend simple;
%simple(dsn="C:\sasdata\somedata",
variable=time1-time4);
The Macro is now in the catalog
Macro Catalog
%macro simple(dsn=,variable=);
proc means data=&DSN;
var &variable;
run;
%mend simple;
Macro Processor
Word Scanner
Symbol Table
Input Stack
Lines
remaining in
input stack
%simple(dsn="C:\sasdata\somedata",
variable=time1-time4);
The Macro Processor recognizes this code as a Macro call, begins
execution, creates a symbol table…
Macro Catalog
%macro simple(dsn=,variable=);
proc means data=&DSN;
var &variable;
run;
%mend simple;
Macro Processor
Word Scanner
Symbol Table
Input Stack
%simple(dsn="C:\sasdata\somedata",
variable=time1-time4);
Dsn
Variable
"C:\sasdata\somedata“
time1-time4
The data=&DSN in put in the input stack, scanned, processed, and the value
of &DSN is read form the symbol table and placed into the macro code.
Macro Catalog
%macro simple(dsn=,variable=);
proc means data="C:\sasdata\somedata“
;
var &variable;
run;
%mend simple;
Macro Processor
Word Scanner
Symbol Table
Input Stack
data=&DSN
Dsn
Variable
"C:\sasdata\somedata“
time1-time4
Once all macro variables are resolved,
code in macro is run
proc means data="C:\sasdata\somedata“;
var time1-time4;
run;
And SAS returns to the remaining code
stream…
Note : %IF, %DO and other macro statements may make the
compile process more complicated. This was a simple example.
Scopes of Macro Variables
• Global – Exist for the duration of the SAS session and can
be referenced anywhere
• Local – exists only during the exception of the macro and
have no value outside the macro
• Scopes can be nested
• Macro variables are stored in symbol tables, and these
tables can have global or local status
• Note: The %SYMEXIST function can be used to
determine if a variable is currently available.
Global Macro Variables Example
%Let address=1028 madison avenue;
data _NULL_;
file print;
put "%upcase(&address)";
put "%propcase(&address)";
put "%scan(&address,1)";
put "%length(&address)";
run;
END