Upload
arthur8898
View
9
Download
3
Embed Size (px)
DESCRIPTION
Macro variables are the most essential part of the SAS macro facility. One can create macro variables by using the %LET statement, the SYMPUT routine, or the INTO clause in the SELECT statement from the SQL procedure. Sometimes a SAS programmer is often unsure when best to use which method due to a lack in understanding each step of macro language processing. This lack in understanding includes how SAS statements are transferred from the input stack to the macro processor and DATA step compiler, what role the macro processor plays during this process, and when best to utilize the interface to interact with the macro facility during the DATA or SQL execution. Once one grasps the intricacies of macro language processing, one will best know how to accurately create a macro variable.
Citation preview
When Best to Use the %LET Statement, the SYMPUT Routine, or the INTO Clause to Create Macro Variables
Arthur LiDepartment of Information Science
City of Hope Comprehensive Cancer Center Duarte, CA
DATA Step PROC Steps
SAS Macro Facility
BASE SAS
INTRODUCTION
Use to generate SAS codes
more…
PROC SQL
DATA Step PROC Steps
SAS Macro Facility
BASE SAS
INTRODUCTION
more…
PROC SQL
SAS Macro Facility -Has its own language
SAS MacroVariables
SAS Macro Programs
Prerequisites:
Focus of this talk
DATA Step PROC Steps
SAS Macro Facility
BASE SAS
INTRODUCTION
more…
PROC SQL
SAS Macro Facility -Has its own language
SAS MacroVariables
SAS Macro Programs
Prerequisites:
SAS MacroVariables
SAS Macro Variables
AutomaticUser-
Defined
Focus of this talk
THE %LET STATEMENT
One way to create a macro variable is to use the %LET statement
%LET MACRO-VARIABLE = VALUE;
%let var1 = 4 + 3;
The VALUE is stored as character strings
MACRO-VARIABLE var1
VALUE 4 + 3
Mathematical expressions are not evaluated
THE %LET STATEMENT
One way to create a macro variable is to use the %LET statement
%LET MACRO-VARIABLE = VALUE;
%let var2 = leading blank;
The VALUE is stored as character strings
MACRO-VARIABLE var2
VALUE leading blank
Leading and trailing blanks are removed
THE %LET STATEMENT
One way to create a macro variable is to use the %LET statement
%LET MACRO-VARIABLE = VALUE;
%let var3 = " quotations ";
The VALUE is stored as character strings
MACRO-VARIABLE var3
VALUE “ quotations ”
Quotation marks are part of the values
THE %LET STATEMENT
One way to create a macro variable is to use the %LET statement
%LET MACRO-VARIABLE = VALUE;
If MACRO-VARIABLE and/or VALUE contain references to another macro variable, the reference will be evaluated first before the assignment
REFERENCING MACRO VARIABLES
Once a macro variable is defined, the value of the macro variable is stored in the global symbol table
%let ht = 63;
Global Symbol Table
SAS Automatic Macro Variables:
SYSDATE 19JUN06
SYSDAY Monday
SYSTIME 10:34
…
…
User-Defined Macro Variables:ht 63
REFERENCING MACRO VARIABLES
To substitute a macro variable in the SAS program, you must reference the macro variable:
%let ht = 63;data ex1; set height; tall = height > &ht;run;
Global Symbol Table
SAS Automatic Macro Variables:
SYSDATE 19JUN06
SYSDAY Monday
SYSTIME 10:34
…
…
User-Defined Macro Variables:ht 63
& MACRO-VARIABLE
63
If the reference of a macro variable is within quotations, the double quotation marks must be used
“& MACRO-VARIABLE”
UNDERSTANDING SAS PROCESSING
In order to understand how macro variables are processed and stored, one needs to understand how SAS processing works
Compilation phaseEach statement is scanned for syntax
errors
Execution phaseThe DATA step reads
and processes the input data
the descriptor portion is created
If there is no syntax error
Done in the compiler
How are the SAS statements transferred to the compiler?
A DATA step is processed in sequence in two phases
UNDERSTANDING SAS PROCESSING
INPUT STACK When a program is submitted, all the codes are placed in a memory area, called the input stack
data ex1; set height; tall = height > 63;run;
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKdata ex1; set height; tall = height > 63;run;
The word scanner takes the statements from the input stack and tokenizes the statements into tokens
The word scanner then directs the tokens to the right location
DATA step compiler Macro processor, etc.
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKex1; set height; tall = height > 63;run;
data
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; set height; tall = height > 63;run;
ex1
data
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKset height; tall = height > 63;run;
;
ex1 data
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKheight; tall = height > 63;run;
set
; ex1
data
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; tall = height > 63;run;
height
set ;
data ex1
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKtall = height > 63;run;
;
height set
data ex1;
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK= height > 63;run;
tall
; height
data ex1; set
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKheight > 63;run; =
tall ;
data ex1; setheight
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK> 63;run; height
= tall
data ex1; setheight;
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK63;run; >
height =
data ex1; setheight; tall
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK;run; 63
> height
data ex1; setheight; tall =
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKrun;
;
63 >
data ex1; setheight; tall =height
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK;
run
; 63
data ex1; setheight; tall =height >
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
;
run ;
data ex1; setheight; tall =height > 63
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
; run
data ex1; setheight; tall =height > 63;
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
;
data ex1; setheight; tall =height > 63; run
UNDERSTANDING SAS PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
data ex1; setheight; tall =height > 63; run;
When the compiler receives the semicolon following the RUN statement, it stops taking tokens from the word scanner
UNDERSTANDING SAS PROCESSING
Compilation phaseEach statement is
scanned for syntax errors.
Execution phaseThe DATA step reads and processes the input data.
If there is no syntax error
UNDERSTANDING SAS PROCESSING
Types of Token Contains … Examples
Literal Characters enclosed in quotation marks “John” ‘John’
Numerals including decimals, E-notation, date, time, datetime constants, and hexadecimal constants
555‘01mar2010’d30e4
Number
Characters that begin with a letter or underscore and that continues with underscores, letters, or numbers. A period can sometimes be part of a name
_n_meansdollar9.2Descending
Name
Characters other than a letter, number, or underscore that have a special meaning to the SAS system
Special character
/ + % & . ;
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK%let ht =63; data ex1; set height; tall =height >&ht; run;
The macro facility includes a macro processor that is responsible for processing all macro language elements
MACRO PROCESSOR
Macro variable references and %LET statements are part of the macro language
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK%let ht =63; data ex1; set height; tall =height >&ht; run;
Macro trigger: %LET name-token & name-token
MACRO PROCESSOR
The word scanner needs to recognize the macro language and direct them to the macro processor
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKlet ht =63; data ex1; set height; tall =height >&ht; run;
%
MACRO PROCESSOR
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKht =63; data ex1; set height; tall =height >&ht; run;
let
%
MACRO PROCESSORWhen the word
scanner detects %LET (a macro trigger), it starts to direct the tokens to the macro processor
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK=63; data ex1; set height; tall =height >&ht; run;
ht
let
MACRO PROCESSOR
%
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK63; data ex1; set height; tall =height >&ht; run;
=
ht
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables
let
%
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKdata ex1; set height; tall =height >&ht; run;
;
63
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables
=
ht
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKex1; set height; tall =height >&ht; run;
data
;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht
63
=
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; set height; tall =height >&ht; run;
ex1
data
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht
;
63
The macro processor stops processing the tokens when the semicolon is encountered
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKset height; tall =height >&ht; run;
;
ex1 data
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKheight; tall =height >&ht; run;
set
; ex1
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; tall =height >&ht; run;
height
set ;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKtall =height >&ht; run; ;
height set
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1;
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK=height >&ht; run; tall
; height
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; set
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKheight >&ht; run; =
tall ;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK>&ht; run;
height
= tall
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setHeight;
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK&ht; run;
>
height =
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKht; run;
&
> height
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
ht
& >
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =height
When the word scanner encounters the ampersand followed by HT, it directs them to the macro processor
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
ht
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
&
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
ht
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
ht
data ex1; setheight; tall =Height >
The macro processor looks up the macro variable HT and takes its value from the symbol take
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
ht
data ex1; setheight; tall =Height >
63
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height >
63
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK63; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK; run;
63
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACKrun;
;
63
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK;
run
; 63
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height >
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
;
run ;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height > 63
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
; run
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height > 63;
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height > 63; run
MACRO PROCESSING
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILER
INPUT STACK
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34...User-Defined Macro Variables ht 63
data ex1; setheight; tall =Height > 63; run;
MACRO PROCESSING
PROCESSING MACRO VARIABLES AT EXECUTION TIME
In many applications, we need to create macro variables during DATA step execution
For example, we might need to create macro variables and assign values to them based on
data values in SAS data sets or in external filesprogramming logiccomputed values
PROCESSING MACRO VARIABLES AT EXECUTION TIME
name sex height tall
1 John m 65 1
2 Tom m 60 0
3 Mary f 62 0
4 Helen f 64 1
Create macro variable TITL - serves as a title for printing the dataset ex1
If there are some students > 63 inches (when tall = 1), TITL = “Some students are taller than 63 inches” If none of the students > than 63 inches, TITL = “None of the students are taller than 63 inches”
Ex1:
PROCESSING MACRO VARIABLES AT EXECUTION TIME
name sex height tall
1 John m 65 1
2 Tom m 60 0
3 Mary f 62 0
4 Helen f 64 1
Ex1:
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl = "Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end;run;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
name sex height tall
1 John m 65 1
2 Tom m 60 0
3 Mary f 62 0
4 Helen f 64 1
Ex1:
proc print data=ex1; title &titl;run;
None of the students are taller than 63 inches
Obs name sex height tall 1 John m 65 1 2 Tom m 60 0 3 Mary f 62 0 4 Helen f 64 1
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl ="Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end; run;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
="Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end; run;
titl
let
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables
%
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
%let titl = "None of the students are taller than 63 inches"; end; end; run;
;
do else
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
let titl = "None of the students are taller than 63 inches"; end; end; run;
%
; do
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
= "None of the students are taller than 63 inches"; end; end; run;
titl
let
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches
%
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
titl = "None of the students are taller than 63 inches"; end; end; run;
let
% ;
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl Some students are taller than 63 inches
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
end; run;;
end
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl None of the students are taller than 63 inches
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
WORD SCANNER
TOKENNIZERDETERMINE
DESTINATION
COMPILERINPUT STACK
MACRO PROCESSOR GLOBAL SYMBOL TABLE
SAS Automatic Macro VariablesSYSDATE 19JUN06SYSDAT MondaySYSTIME 10:34..User-Defined Macro Variables Titl None of the students are taller than 63 inches
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do;end; else do; end; end; run;
PROCESSING MACRO VARIABLES AT EXECUTION TIME
PROCESSING MACRO VARIABLES AT EXECUTION TIME
name sex height tall
1 John m 65 1
2 Tom m 60 0
3 Mary f 62 0
4 Helen f 64 1
Ex1:
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; %let titl = "Some students are taller than 63 inches"; end; else do; %let titl = "None of the students are taller than 63 inches"; end; end;run;
Here is what the program will look like when it is processed by the compiler
THE SYMPUT ROUTINE
To create a macro variable during the DATA step execution, use the SYMPUT routine
CALL SYMPUT (MACRO-VARIABLE, VALUE);
Both MACRO-VARIABLE and VALUE can be specified as
literal (text in quotations) a DATA step variable a DATA step expression
THE SYMPUT ROUTINE
CALL SYMPUT (MACRO-VARIABLE, VALUE);
Both arguments are literals
‘MACRO-VARIABLE’:Text enclosed in the quotation marks is the exact macro variable name
‘VALUE’:The exact value that is assigned to the MACRO-VARIABLE
THE SYMPUT ROUTINE
data _null_; set ex1 end=last; if tall then count_tall +1; if last then do; if count_tall then do; call symput('titl', 'Some students are taller than 63 inches'); end; else do; call symput('titl', 'None of the students are taller than 63 inches'); end; end;run;
Use the SUMPUT routine to fix the previous program
THE SYMPUT ROUTINE
proc print data=ex1; title &titl;run;
Some students are taller than 63 inches
Obs name sex height tall 1 John m 65 1 2 Tom m 60 0 3 Mary f 62 0 4 Helen f 64 1
THE SYMPUT ROUTINE
CALL SYMPUT (MACRO-VARIABLE, VALUE);
When VALUE is a DATA step variable
A DATA step variable; not in quotation marks
You are assigning the value of a DATA step variable to the MACRO-VARIABLE
Any leading or trailing blanks that are part of the values of a DATA step variable will be part of the macro variables
For a numeric variable, the values will be converted to character variables by using the BEST12. format
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Suppose that you would like to create four macro variables:
Height:
Macro Variable Names Values
John_ht 65
Tom_ht 60
Mary_ht 62
Helen_ht 64
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
data _null_; set height; if name = 'John' then call symput ('John_ht', height); else if name = 'Tom' then call symput ('Tom_ht', height); else if name = 'Mary' then call symput ('Mary_ht', height); else if name = 'Helen' then call symput ('Helen_ht', height);run;
Height:
HEIGHT is not in quotation marks
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
%put macro variable John_ht: &John_ht;%put macro variable Tom_ht: &Tom_ht;%put macro variable Mary_ht: &Mary_ht;%put macro variable Helen_ht: &Helen_ht;
Height:
359 %put macro variable John_ht: &John_ht;macro variable John_ht: 65360 %put macro variable Tom_ht: &Tom_ht;macro variable Tom_ht: 60361 %put macro variable Mary_ht: &Mary_ht;macro variable Mary_ht: 62362 %put macro variable Helen_ht: &Helen_ht;macro variable Helen_ht: 64
Log:
THE SYMPUT ROUTINE
CALL SYMPUT (MACRO-VARIABLE, VALUE);
When MACRO-VARIABLE is a DATA step variable
A DATA step variable; not in quotation marks
You are creating multiple macro variables The names of the macro variables are the values of a
DATA step variable
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Suppose that you would like to create four macro variables:
Height:
Macro Variable Names Values
John 65
Tom 60
Mary 62
Helen 64
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Height:
data _null_; set height; call symput (name, height);run;
NAME is not in quotation marks
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Height:
%put macro variable John: &John;%put macro variable Tom: &Tom;%put macro variable Mary: &Mary;%put macro variable Helen: &Helen;
368 %put macro variable John: &John;macro variable John: 65369 %put macro variable Tom: &Tom;macro variable Tom: 60370 %put macro variable Mary: &Mary;macro variable Mary: 62371 %put macro variable Helen: &Helen;macro variable Helen: 64
Log:
Notice that “blanks” are part of the macro variables
THE SYMPUT ROUTINE
CALL SYMPUT (MACRO-VARIABLE, VALUE);
DATA step expressions can be used in one/both arguments
A DATA step expression
A DATA step expression
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Suppose that you would like to create four macro variables:
Height:
Macro Variable Names Values
height_John 65
height_Tom 60
height_Mary 62
height_Helen 64
Also, remove the leading blanks
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Height:
data _null_; set height; call symput ("height_" ||name, trim(left(height)));run;
THE SYMPUT ROUTINE
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Height:
%put macro variable height_John: &height_John;%put macro variable height_Tom: &height_Tom;%put macro variable height_Mary: &height_Mary;%put macro variable height_Helen: &height_Helen;
411 %put macro variable height_John: &height_John;macro variable height_John: 65412 %put macro variable height_Tom: &height_Tom;macro variable height_Tom: 60413 %put macro variable height_Mary: &height_Mary;macro variable height_Mary: 62414 %put macro variable height_Helen: &height_Helen;macro variable height_Helen: 64
Log:
THE SYMPUTX ROUTINE
The SYMPUTX routine is an improved version of the SYMPUT routine
The SYMPUTX routine can remove leading and trailing blanks from both arguments
For example, the previous program can be re-written as below:
data _null_; set height; call symputx ("height_" ||name, height);run;
THE SYMPUTX ROUTINE
%put macro variable height_John: &height_John;%put macro variable height_Tom: &height_Tom;%put macro variable height_Mary: &height_Mary;%put macro variable height_Helen: &height_Helen;
429 %put macro variable height_John: &height_John;macro variable height_John: 65430 %put macro variable height_Tom: &height_Tom;macro variable height_Tom: 60431 %put macro variable height_Mary: &height_Mary;macro variable height_Mary: 62432 %put macro variable height_Helen: &height_Helen;macro variable height_Helen: 64
Log:
THE SYMPUTX ROUTINE
Difference between CALL SYMPUTX and CALL SYMPUT
CALL SYMPUTX CALL SYMPUT
Does not write a note to the SAS log when the 2nd argument is numeric
Writes a note to the SAS log when the 2nd argument is numeric
Uses a field width of up to 32 characters for converting a numeric 2nd argument to characters
Uses a field width of up to 12 characters for converting a numeric 2nd argument to characters
Left-justifies both arguments and trims trailing blanks
Does not left-justify the arguments and trims trailing blanks from the first argument only
Enables you to specify the symbol table
Does not enable you to specify the symbol table
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
You can create/update macro variables during the execution of PROC SQL
The INTO clause in the SELECT statement performs a similar role to the SYMPUT(X) routine in the DATA step
The INTO clausecan create one or more macro variables can assign a calculated result or the value of a data
variable to a macro variablecan only be used in the outer query of a SELECT
statement cannot be used when you are creating a table or a
view
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Here is the general syntax for PROC SQL and the INTO clause:
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;
To select one or more columns from SQL table/view, you can specify column1, column2, … after the keyword SELECT
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Here is the general syntax for PROC SQL and the INTO clause:
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;
Macro-variable1, macro-variable2 are the names of the macro variables that you are creating
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Here is the general syntax for PROC SQL and the INTO clause:
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;
You must write colon(s) (:) before each of the macro variables
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Here is the general syntax for PROC SQL and the INTO clause:
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;
The INTO clause does not trim the leading and trailing blanks of the macro variables
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Here is the general syntax for PROC SQL and the INTO clause:
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1 <, :macro-variable2, …>FROM table |view<other clauses>; QUIT;
To suppress display-output from PROC SQL, you can use the NOPRINT option
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
You can assign a calculated summary statistic to a macro variable
Suppose you would like to create a macro variable MEAN_HT - average heights of students
proc sql noprint; select mean(height) into: mean_ht from height;quit;
%put macro variable mean_ht: &mean_ht;
523 %put macro variable mean_ht: &mean_ht;macro variable mean_ht: 62.75Log:
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Without using the PROC SQL, you must use multiple steps to accomplish this task
proc means data=height noprint; var height; output out=height_mean mean=ht_mean;run;
data _null_; set height_mean; call symputx('mean_ht1', ht_mean);run;
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
In the previous program, we created four macro variables:data _null_; set height; if name = 'John' then call symput ('John_ht', height); else if name = 'Tom' then call symput ('Tom_ht', height); else if name = 'Mary' then call symput ('Mary_ht', height); else if name = 'Helen' then call symput ('Helen_ht', height);run;
We can use PROC SQL to achive the same taskproc sql noprint; select height into: John_ht1 from height where name = 'John'; select height into: Tom_ht1 from height where name = 'Tom'; select height into: Mary_ht1 from height where name = 'Mary'; select height into: Helen_ht1 from height where name = 'Helen';quit;
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
You can create a range of macro variablesEach of the macro variables will contain each row in the
result of the SELECT statement
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
name1name2name3name4
height1height2height3height4
into :name1 - :name4, :height1 - :height4
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …>INTO :macro-variable1_1 - :macro-variable1_n < NOTRIM> <, :macro-variable2_1 - :macro-variable2_n < NOTRIM>, …>FROM table |view<other clauses>; QUIT;
By default, the leading and trailing blanks are removed from values before they are stored in macro variables
If you don’t want to remove the leading and trailing blanks, you can use the NOTRIM option
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heightsproc sql noprint; select name, height into :name1 - :name4,
:height1 - :height4 from height;quit;
%put macro variable name1: &name1;%put macro variable name2: &name2;%put macro variable name3: &name3;%put macro variable name4: &name4;%put macro variable height1: &height1;%put macro variable height2: &height2;%put macro variable height3: &height3;%put macro variable height4: &height4;
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heights663 %put macro variable name1: &name1;macro variable name1: John664 %put macro variable name2: &name2;macro variable name2: Tom665 %put macro variable name3: &name3;macro variable name3: Mary666 %put macro variable name4: &name4;macro variable name4: Helen667 %put macro variable height1: &height1;macro variable height1: 65668 %put macro variable height2: &height2;macro variable height2: 60669 %put macro variable height3: &height3;macro variable height3: 62670 %put macro variable height4: &height4;macro variable height4: 64
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
When creating a range of macro variables, PROC SQL restricts the name convention for the macro variables
The names of the macro variables must end with an integer with a valid range, such as HEIGHT1 – HEIGHT4
You won’t be able to use more meaningful names such as JOHN_HT, TOM_HT, MARY_HT, and HELEN_HT
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
You can use the INTO clause to create a macro variable that holds all the values of a column by concatenating them and separating them by a delimiter
PROC SQL <PRINT|NOPRINT>;SELECT column1 <, column2, …> INTO :macro-variable1 SEPARATED BY ‘delimiter1’ <, :macro-variable2 SEPARATED BY ‘delimiter2’, …>FROM table |view<other clauses>; QUIT;
Delimiter1 is used to separate all the values in the column and must be enclosed in quotation marks
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
Create macro variables
name sex height
1 John m 65
2 Tom m 60
3 Mary f 62
4 Helen f 64
Macro Variable Value
NAMELIST John Tom Mary Helen
HEIGHTLIST 65,60,62,64
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
proc sql noprint; select name, height
into : namelist separated by ' ', : heightlist separated by ','
from height;quit;
%put macro variable namelist: &namelist;%put macro variable heightlist: &heightlist;
871 %put macro variable namelist: &namelist;macro variable namelist: John Tom Mary Helen872 %put macro variable heightlist: &heightlist;macro variable heightlist: 65,60,62,64
CONCLUSION
Creating a macro variable by using the %LET statement occurs before the execution of any other SAS language statements
To create a macro variable during the DATA step execution, you must use either the SYMPUT or SYMPUTX routines
To create a macro variable during the execution of PROC SQL, you must use the INTO clause
Understanding the mechanisms of creating macro variables is an important foundation for learning how best to write macro programs
REFERENCES
Burlew, Michele M. SAS® Macro Programming Made Easy, 2nd Edition.
SAS Online Doc® 9.1.3. Cary, NC: SAS Institute Inc.
ACKNOWLEDGEMENT
I would like to thank Kathryn McLawhorn and Scott McElroy, Technical Support Analysts from SAS Technical Support, for their valuable programming suggestions and insight
CONTACT INFORMATION
Arthur Li
City of Hope
Division of Information Science
1500 East Duarte Road
Duarte, CA 91010 - 3000
Phone: (626) 256-4673 ext. 65121
E-mail: [email protected]