The document discusses different methods for creating macro variables in SAS, including the %LET statement, SYMPUT routine, and INTO clause. It explains that the %LET statement stores values as character strings and does not evaluate mathematical expressions. It also covers how SAS processing works, with statements being sent to the word scanner, tokenizer, compiler, and macro processor. The macro processor is responsible for processing macro language elements like %LET and macro variable references.
Micro-Scholarship, What it is, How can it help me.pdf
When best to use the %let statement, the symput routine, or the into clause to create macro variables
1. When Best to Use the %LET Statement, the SYMPUT Routine, or the INTO Clause to Create Macro Variables Arthur Li Department of Information Science City of Hope Comprehensive Cancer Center Duarte, CA
2. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS Use to generate SAS codes more… PROC SQL
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14. data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ex1; set height; tall = height > 63 ; run ;
15. ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; set height; tall = height > 63 ; run ;
16. ; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK set height; tall = height > 63 ; run ;
17. set ; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height; tall = height > 63 ; run ;
18. height set ; data ex1 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; tall = height > 63 ; run ;
19. ; height set data ex1; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK tall = height > 63 ; run ;
20. tall ; height data ex1; set UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK = height > 63 ; run ;
21. = tall ; data ex1; set height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height > 63 ; run ;
22. height = tall data ex1; set height; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK > 63 ; run ;
23. > height = data ex1; set height; tall UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK 63 ; run ;
24. 63 > height data ex1; set height; tall = UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ;
25. ; 63 > data ex1; set height; tall = height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK run ;
26. run ; 63 data ex1; set height; tall = height > UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ;
27. ; run ; data ex1; set height; tall = height > 63 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
28. ; run data ex1; set height; tall = height > 63 ; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
29. ; data ex1; set height; tall = height > 63 ; run UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
30.
31. 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’d 30e4 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_ means dollar9.2 Descending Name Characters other than a letter, number, or underscore that have a special meaning to the SAS system Special character / + % & . ;
32.
33.
34. % MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK let ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
35.
36. ht let % MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
37. = ht let % MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK 63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables
38. ; 63 = ht MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables
39. data ; 63 = MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht
40.
41. ; ex1 data MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK set height; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
42. set ; ex1 data MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
43. height set ; data ex1 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
44. ; height set data ex1; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK tall =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
45. tall ; height data ex1; set MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK =height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
46. = tall ; data ex1; set height MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
47. height = tall data ex1; set Height; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK >&ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
48. > height = data ex1; set height; tall MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK &ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
49. & > height data ex1; set height; tall = MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ht; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
50.
51. ht & data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
52. ht data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
53.
54. ht data ex1; set height; tall = Height > 63 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
55. data ex1; set height; tall = Height > 63 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
56. data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK 63 ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
57. 63 data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
58. ; 63 data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
59. run ; 63 data ex1; set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
60. ; run ; data ex1; set height; tall = Height > 63 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
61. ; run data ex1; set height; tall = Height > 63 ; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
62. ; data ex1; set height; tall = Height > 63 ; run MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
63. data ex1; set height; tall = Height > 63 ; run; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . . User-Defined Macro Variables ht 63
64.
65.
66. PROCESSING MACRO VARIABLES AT EXECUTION TIME 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 ; 64 62 60 65 height 1 f Helen 4 0 f Mary 3 0 m Tom 2 1 m John 1 tall sex name
67. PROCESSING MACRO VARIABLES AT EXECUTION TIME 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 64 62 60 65 height 1 f Helen 4 0 f Mary 3 0 m Tom 2 1 m John 1 tall sex name
68. PROCESSING MACRO VARIABLES AT EXECUTION TIME WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT 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 Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables
69. titl let % 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT 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 ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables
70. ; do else 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK %let titl = "None of the students are taller than 63 inches"; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl Some students are taller than 63 inches
71. % ; do 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK let titl = "None of the students are taller than 63 inches"; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl Some students are taller than 63 inches
72. titl let % 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK = "None of the students are taller than 63 inches"; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl Some students are taller than 63 inches
73. let % ; 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK titl = "None of the students are taller than 63 inches"; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl Some students are taller than 63 inches
74. ; end 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl None of the students are taller than 63 inches
75. 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 WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables Titl None of the students are taller than 63 inches
76. PROCESSING MACRO VARIABLES AT EXECUTION TIME 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 64 62 60 65 height 1 f Helen 4 0 f Mary 3 0 m Tom 2 1 m John 1 tall sex name
77.
78.
79.
80. 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
81.
82.
83. THE SYMPUT ROUTINE 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 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
84. THE SYMPUT ROUTINE %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: 65 360 %put macro variable Tom_ht: &Tom_ht; macro variable Tom_ht: 60 361 %put macro variable Mary_ht: &Mary_ht; macro variable Mary_ht: 62 362 %put macro variable Helen_ht: &Helen_ht; macro variable Helen_ht: 64 Log: 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
85.
86.
87. THE SYMPUT ROUTINE Height: data _null_ ; set height; call symput (name, height); run ; NAME is not in quotation marks 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
88. THE SYMPUT ROUTINE 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: 65 369 %put macro variable Tom: &Tom; macro variable Tom: 60 370 %put macro variable Mary: &Mary; macro variable Mary: 62 371 %put macro variable Helen: &Helen; macro variable Helen: 64 Log: Notice that “blanks” are part of the macro variables 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
89.
90.
91. THE SYMPUT ROUTINE Height: data _null_ ; set height; call symput ( "height_" ||name, trim(left(height))); run ; 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
92. THE SYMPUT ROUTINE 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: 65 412 %put macro variable height_Tom: &height_Tom; macro variable height_Tom: 60 413 %put macro variable height_Mary: &height_Mary; macro variable height_Mary: 62 414 %put macro variable height_Helen: &height_Helen; macro variable height_Helen: 64 Log: 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
106. 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 ; into :name1 - :name4, :height1 - :height4 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name name1 name2 name3 name4 height1 height2 height3 height4