When best to use the %let statement, the symput routine, or the into clause to create macro variables
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

When best to use the %let statement, the symput routine, or the into clause to create macro variables

  • 1,503 views
Uploaded on

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......

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.

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Thanx a lot.... U saved my ass at my job....

    I know macros, but not very fundamental information... My boss asked me to explain SAS Macros to some of my juniors about 20 on sas macros... and my concepts were not very clear... I had 3 hours....

    Google has so much information and so scattered...I was like almost dead..Thanks to you i was able to do a good job
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
1,503
On Slideshare
1,501
From Embeds
2
Number of Embeds
2

Actions

Shares
Downloads
37
Comments
1
Likes
0

Embeds 2

http://www.linkedin.com 1
https://www.linkedin.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS more… PROC SQL
    • SAS Macro Facility
    • Has its own language
    SAS Macro Variables SAS Macro Programs Prerequisites: Focus of this talk
  • 4. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS more… PROC SQL
    • SAS Macro Facility
    • Has its own language
    SAS Macro Variables SAS Macro Programs Prerequisites: SAS Macro Variables SAS Macro Variables Automatic User- Defined Focus of this talk
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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; ht 63 User-Defined Macro Variables: … … SYSTIME 10:34 SYSDAY Monday SYSDATE 19JUN06 SAS Automatic Macro Variables: Global Symbol Table
  • 10. 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 ; ht 63 & MACRO-VARIABLE 63
    • If the reference of a macro variable is within quotations, the double quotation marks must be used
    “ & MACRO-VARIABLE” User-Defined Macro Variables: … … SYSTIME 10:34 SYSDAY Monday SYSDATE 19JUN06 SAS Automatic Macro Variables: Global Symbol Table
  • 11. UNDERSTANDING SAS PROCESSING
    • In order to understand how macro variables are processed and stored, one needs to understand how SAS processing works
    Compilation phase Each statement is scanned for syntax errors Execution phase The 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
  • 12. UNDERSTANDING SAS PROCESSING
    • 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 ; INPUT STACK
  • 13.
    • 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 TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK data ex1; set height; tall = height > 63 ; run ;
  • 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. data ex1; set height; 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 phase Each statement is scanned for syntax errors. Execution phase The DATA step reads and processes the input data. If there is no syntax error WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
  • 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.
    • The macro facility includes a macro processor that is responsible for processing all macro language elements
    • Macro variable references and %LET statements are part of the macro language
    MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK %let ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  • 33.
    • Macro trigger:
    • %LET name-token
    • & name-token
    • The word scanner needs to recognize the macro language and direct them to the macro processor
    MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK %let ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  • 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. let %
    • When the word scanner detects %LET (a macro trigger), it starts to direct the tokens to the macro processor
    MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  • 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. ex1 data ; 63
    • The macro processor stops processing the tokens when the semicolon is encountered
    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
  • 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. ht & > data ex1; set height; tall = height
    • When the word scanner encounters the ampersand followed by HT, it directs them to the macro processor
    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
  • 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. ht data ex1; set height; tall = Height >
    • The macro processor looks up the macro variable HT and takes its value from the symbol take
    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
  • 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. 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 files
      • programming logic
      • computed values
  • 65. PROCESSING MACRO VARIABLES AT EXECUTION TIME
    • 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: 64 62 60 65 height 1 f Helen 4 0 f Mary 3 0 m Tom 2 1 m John 1 tall sex name
  • 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. 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
  • 78. 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
  • 79. 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
  • 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. 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
  • 82. THE SYMPUT ROUTINE
    • Suppose that you would like to create four macro variables:
    Height: 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name 64 Helen_ht 62 Mary_ht 60 Tom_ht 65 John_ht Values Macro Variable Names
  • 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. 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
  • 86. THE SYMPUT ROUTINE
    • Suppose that you would like to create four macro variables:
    Height: 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name 64 Helen 62 Mary 60 Tom 65 John Values Macro Variable Names
  • 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. 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
  • 90. THE SYMPUT ROUTINE
    • Suppose that you would like to create four macro variables:
    Height:
    • Also, remove the leading blanks
    64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name 64 height_Helen 62 height_Mary 60 height_Tom 65 height_John Values Macro Variable Names
  • 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
  • 93. 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 ;
  • 94. 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: 65 430 %put macro variable height_Tom: &height_Tom; macro variable height_Tom: 60 431 %put macro variable height_Mary: &height_Mary; macro variable height_Mary: 62 432 %put macro variable height_Helen: &height_Helen; macro variable height_Helen: 64 Log:
  • 95. 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 2 nd argument is numeric Writes a note to the SAS log when the 2 nd argument is numeric Uses a field width of up to 32 characters for converting a numeric 2 nd argument to characters Uses a field width of up to 12 characters for converting a numeric 2 nd 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
  • 96. 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 clause
      • can create one or more macro variables
      • can assign a calculated result or the value of a data variable to a macro variable
      • can only be used in the outer query of a SELECT statement
      • cannot be used when you are creating a table or a view
  • 97. 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
  • 98. 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
  • 99. 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
  • 100. 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
  • 101. 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
  • 102. 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.75 Log:
  • 103. 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 ;
  • 104. 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 task
    proc 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 ;
  • 105. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
    • You can create a range of macro variables
    • Each 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 ;
  • 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
  • 107. 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
  • 108. 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 heights
    proc 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;
  • 109. 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 heights
    663 %put macro variable name1: &name1; macro variable name1: John 664 %put macro variable name2: &name2; macro variable name2: Tom 665 %put macro variable name3: &name3; macro variable name3: Mary 666 %put macro variable name4: &name4; macro variable name4: Helen 667 %put macro variable height1: &height1; macro variable height1: 65 668 %put macro variable height2: &height2; macro variable height2: 60 669 %put macro variable height3: &height3; macro variable height3: 62 670 %put macro variable height4: &height4; macro variable height4: 64
  • 110. 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
  • 111. 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
  • 112. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL
    • Create macro variables
    Macro Variable Value NAMELIST John Tom Mary Helen HEIGHTLIST 65,60,62,64 64 62 60 65 height f Helen 4 f Mary 3 m Tom 2 m John 1 sex name
  • 113. 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 Helen 872 %put macro variable heightlist: &heightlist; macro variable heightlist: 65,60,62,64
  • 114. 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
  • 115. REFERENCES
    • Burlew, Michele M. SAS ® Macro Programming Made Easy, 2 nd Edition.
    • SAS Online Doc ® 9.1.3. Cary, NC: SAS Institute Inc.
  • 116. 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
  • 117. 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: xueli@coh.org