When Best to Use the %LET Statement, the SYMPUT Routine, or the INTO Clause to Create Macro Variables Arthur Li Department...
INTRODUCTION DATA Step  PROC Steps SAS Macro  Facility BASE SAS Use to generate SAS codes more… PROC SQL
INTRODUCTION DATA Step  PROC Steps SAS Macro  Facility BASE SAS more… PROC SQL <ul><li>SAS Macro Facility   </li></ul><ul>...
INTRODUCTION DATA Step  PROC Steps SAS Macro  Facility BASE SAS more… PROC SQL <ul><li>SAS Macro Facility   </li></ul><ul>...
THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement  </li></ul>%LET  MACRO-VARIABLE...
THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement  </li></ul>%LET  MACRO-VARIABLE...
THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement  </li></ul>%LET  MACRO-VARIABLE...
THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement  </li></ul>%LET  MACRO-VARIABLE...
REFERENCING MACRO VARIABLES <ul><li>Once a macro variable is defined, the value of the macro variable is stored in the glo...
REFERENCING MACRO VARIABLES <ul><li>To substitute a macro variable in the SAS program, you must reference the macro variab...
UNDERSTANDING SAS PROCESSING <ul><li>In order to understand how macro variables are processed and stored, one needs to und...
UNDERSTANDING SAS PROCESSING <ul><li>When a program is submitted, all the codes are placed in a memory area, called the in...
<ul><li>The word scanner takes the statements from the input stack and tokenizes the statements into tokens </li></ul><ul>...
data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ex1;  set  height; ta...
ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ;  set  height; t...
; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK set  height; ta...
set ; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height; tal...
height set ; data  ex1 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; t...
; height set data  ex1; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ta...
tall ; height data  ex1;  set UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT ST...
= tall ; data  ex1;  set height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT ...
height = tall data  ex1;  set height; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER ...
> height = data  ex1;  set height; tall UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILE...
63 > height data  ex1;  set height; tall = UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMP...
; 63 > data  ex1;  set height; tall = height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION CO...
run ; 63 data  ex1;  set height; tall = height > UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATIO...
; run ; data  ex1;  set height; tall = height >  63 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINA...
; run data  ex1;  set height; tall = height >  63 ; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINA...
; data  ex1;  set height; tall = height >  63 ;  run UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTIN...
data  ex1;  set height; tall = height >  63 ;  run ; <ul><li>When the compiler receives the semicolon following the RUN st...
UNDERSTANDING SAS PROCESSING Types of Token  Contains …  Examples  Literal  Characters enclosed in quotation marks “ John”...
<ul><li>The macro facility includes a macro processor that is responsible for processing all macro language elements </li>...
<ul><li>Macro trigger: </li></ul><ul><li>%LET name-token </li></ul><ul><li>& name-token   </li></ul><ul><li>The word scann...
% MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK let  ht =63;  data  ex1;  set  heigh...
let % <ul><li>When the word scanner detects %LET (a macro trigger), it starts to direct the tokens to the macro processor ...
ht let % MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK =63;  data  ex1;  set  height...
= ht let % MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK 63;  data  ex1;  set  heigh...
; 63 = ht MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK data  ex1;  set  height; tal...
data ; 63 = MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ex1;  set  height; tall =h...
ex1 data ; 63 <ul><li>The macro processor stops processing the tokens when the semicolon is encountered </li></ul>MACRO PR...
; ex1 data MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK set  height; tall =height >...
set ; ex1 data MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height; tall =height >&...
height set ; data  ex1 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; tall =height ...
; height set data  ex1; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK tall =height >...
tall ; height data  ex1;  set MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK =height ...
= tall ; data  ex1;  set height MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height...
height = tall data  ex1;  set Height; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ...
> height = data  ex1;  set height; tall MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STAC...
& > height data  ex1;  set height; tall = MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT ST...
ht & > data  ex1;  set height; tall = height <ul><li>When the word scanner encounters the ampersand followed by HT, it dir...
ht & data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT...
ht data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT S...
ht data  ex1;  set height; tall = Height > <ul><li>The macro processor looks up the macro variable HT and takes its value ...
ht data  ex1;  set height; tall = Height > 63   MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER IN...
data  ex1;  set height; tall = Height > 63   MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT...
data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STAC...
63 data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT S...
; 63 data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT...
run ; 63 data  ex1;  set height; tall = Height > MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER I...
; run ; data  ex1;  set height; tall = Height >  63 MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILE...
; run data  ex1;  set height; tall = Height >  63 ; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILE...
; data  ex1;  set height; tall = Height >  63 ;  run MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPIL...
data  ex1;  set height; tall = Height >  63 ;  run; MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILE...
PROCESSING MACRO VARIABLES AT EXECUTION TIME <ul><li>In many applications, we need to create macro variables during DATA s...
PROCESSING MACRO VARIABLES AT EXECUTION TIME <ul><li>Create macro variable TITL - serves as a title for printing the datas...
PROCESSING MACRO VARIABLES AT EXECUTION TIME Ex1: data   _null_ ; set  ex1 end=last; if  tall  then  count_tall + 1 ; if  ...
PROCESSING MACRO VARIABLES AT EXECUTION TIME Ex1: proc   print   data =ex1; title  &titl; run ; None of the students are t...
PROCESSING MACRO VARIABLES AT EXECUTION TIME WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK data   _nu...
titl let % data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  t...
; do else data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  th...
% ; do data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  then ...
titl let % data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  t...
let % ; data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  then...
; end data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  then d...
data   _null_ ;  set  ex1 end=last;  if  tall  then  count_tall + 1 ;  if  last  then   do ;  if  count_tall  then do ; en...
PROCESSING MACRO VARIABLES AT EXECUTION TIME Ex1: data   _null_ ; set  ex1 end=last; if  tall  then  count_tall + 1 ; if  ...
THE SYMPUT ROUTINE  <ul><li>To create a macro variable during the DATA step execution, use the SYMPUT routine </li></ul>CA...
THE SYMPUT ROUTINE  CALL SYMPUT  (MACRO-VARIABLE, VALUE); <ul><li>Both arguments are literals </li></ul>‘ MACRO-VARIABLE’:...
THE SYMPUT ROUTINE  data   _null_ ; set  ex1 end=last; if  tall  then  count_tall + 1 ; if  last  then   do ; if  count_ta...
THE SYMPUT ROUTINE  proc   print   data =ex1; title  &titl; run ; Some students are taller than 63 inches Obs  name  sex  ...
THE SYMPUT ROUTINE  CALL SYMPUT  (MACRO-VARIABLE, VALUE); <ul><li>When VALUE is a DATA step variable </li></ul>A DATA step...
THE SYMPUT ROUTINE  <ul><li>Suppose that you would like to create four macro variables:  </li></ul>Height: 64 62 60 65 hei...
THE SYMPUT ROUTINE  data   _null_ ; set  height; if  name =  'John'   then   call  symput ( 'John_ht' ,  height); else   i...
THE SYMPUT ROUTINE  %put  macro variable John_ht: &John_ht; %put  macro variable Tom_ht: &Tom_ht; %put  macro variable Mar...
THE SYMPUT ROUTINE  CALL SYMPUT  (MACRO-VARIABLE, VALUE); <ul><li>When MACRO-VARIABLE is a DATA step variable </li></ul>A ...
THE SYMPUT ROUTINE  <ul><li>Suppose that you would like to create four macro variables: </li></ul>Height: 64 62 60 65 heig...
THE SYMPUT ROUTINE  Height: data   _null_ ; set  height; call  symput (name, height); run ; NAME is not in quotation marks...
THE SYMPUT ROUTINE  Height: %put  macro variable John: &John; %put  macro variable Tom: &Tom; %put  macro variable Mary: &...
THE SYMPUT ROUTINE  CALL SYMPUT  (MACRO-VARIABLE, VALUE); <ul><li>DATA step expressions can be used in one/both arguments ...
THE SYMPUT ROUTINE  <ul><li>Suppose that you would like to create four macro variables: </li></ul>Height: <ul><li>Also, re...
THE SYMPUT ROUTINE  Height: data   _null_ ; set  height; call  symput ( &quot;height_&quot;  ||name, trim(left(height))); ...
THE SYMPUT ROUTINE  Height: %put  macro variable height_John: &height_John; %put  macro variable height_Tom: &height_Tom; ...
THE SYMPUTX ROUTINE  <ul><li>The SYMPUTX routine is an improved version of the SYMPUT routine  </li></ul><ul><li>The SYMPU...
THE SYMPUTX ROUTINE  %put  macro variable height_John: &height_John; %put  macro variable height_Tom: &height_Tom; %put  m...
THE SYMPUTX ROUTINE  <ul><li>Difference between CALL SYMPUTX and CALL SYMPUT </li></ul>CALL SYMPUTX  CALL SYMPUT  Does  no...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>You can create/update macro variables during the  exe...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Here is the general syntax for PROC SQL and the INTO ...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Here is the general syntax for PROC SQL and the INTO ...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Here is the general syntax for PROC SQL and the INTO ...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Here is the general syntax for PROC SQL and the INTO ...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Here is the general syntax for PROC SQL and the INTO ...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>You can assign a calculated summary statistic to a ma...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Without using the PROC SQL, you must use multiple ste...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>In the previous program, we created four macro variab...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>You can create a range of macro variables </li></ul><...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  PROC SQL  <PRINT|NOPRINT>; SELECT  column1 <, column2, …> INT...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  PROC SQL  <PRINT|NOPRINT>; SELECT  column1 <, column2, …> INT...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Suppose that you would like to create macro variables...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Suppose that you would like to create macro variables...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>When creating a range of macro variables, PROC SQL re...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>You can use the INTO clause to create a macro variabl...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>Create macro variables </li></ul>Macro Variable Value...
PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL  <ul><li>proc   sql   noprint ; </li></ul><ul><li>select  name...
CONCLUSION <ul><li>Creating a macro variable by using the %LET statement occurs before the execution of any other SAS lang...
REFERENCES <ul><li>Burlew, Michele M.  SAS ®  Macro Programming Made Easy, 2 nd  Edition. </li></ul><ul><li>SAS Online Doc...
ACKNOWLEDGEMENT  <ul><li>I would like to thank Kathryn McLawhorn and Scott McElroy, Technical Support Analysts from SAS Te...
CONTACT INFORMATION <ul><li>Arthur Li </li></ul><ul><li>City of Hope </li></ul><ul><li>Division of Information Science </l...
Upcoming SlideShare
Loading in...5
×

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

1,307

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

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • 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
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
1,307
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 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. 2. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS Use to generate SAS codes more… PROC SQL
  3. 3. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS more… PROC SQL <ul><li>SAS Macro Facility </li></ul><ul><li>Has its own language </li></ul>SAS Macro Variables SAS Macro Programs Prerequisites: Focus of this talk
  4. 4. INTRODUCTION DATA Step PROC Steps SAS Macro Facility BASE SAS more… PROC SQL <ul><li>SAS Macro Facility </li></ul><ul><li>Has its own language </li></ul>SAS Macro Variables SAS Macro Programs Prerequisites: SAS Macro Variables SAS Macro Variables Automatic User- Defined Focus of this talk
  5. 5. THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement </li></ul>%LET MACRO-VARIABLE = VALUE; %let var1 = 4 + 3; <ul><li>The VALUE is stored as character strings </li></ul>MACRO-VARIABLE var1 VALUE 4 + 3 <ul><li>Mathematical expressions are not evaluated </li></ul>
  6. 6. THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement </li></ul>%LET MACRO-VARIABLE = VALUE; %let var2 = leading blank; <ul><li>The VALUE is stored as character strings </li></ul>MACRO-VARIABLE var2 VALUE leading blank <ul><li>Leading and trailing blanks are removed </li></ul>
  7. 7. THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement </li></ul>%LET MACRO-VARIABLE = VALUE; %let var3 = &quot; quotations &quot;; <ul><li>The VALUE is stored as character strings </li></ul>MACRO-VARIABLE var3 VALUE “ quotations ” <ul><li>Quotation marks are part of the values </li></ul>
  8. 8. THE %LET STATEMENT <ul><li>One way to create a macro variable is to use the %LET statement </li></ul>%LET MACRO-VARIABLE = VALUE; <ul><li>If MACRO-VARIABLE and/or VALUE contain references to another macro variable, the reference will be evaluated first before the assignment </li></ul>
  9. 9. REFERENCING MACRO VARIABLES <ul><li>Once a macro variable is defined, the value of the macro variable is stored in the global symbol table </li></ul>%let ht = 63; ht 63 User-Defined Macro Variables: … … SYSTIME 10:34 SYSDAY Monday SYSDATE 19JUN06 SAS Automatic Macro Variables: Global Symbol Table
  10. 10. REFERENCING MACRO VARIABLES <ul><li>To substitute a macro variable in the SAS program, you must reference the macro variable: </li></ul>%let ht = 63; data ex1; set height; tall = height > &ht; run ; ht 63 & MACRO-VARIABLE 63 <ul><li>If the reference of a macro variable is within quotations, the double quotation marks must be used </li></ul>“ & MACRO-VARIABLE” User-Defined Macro Variables: … … SYSTIME 10:34 SYSDAY Monday SYSDATE 19JUN06 SAS Automatic Macro Variables: Global Symbol Table
  11. 11. UNDERSTANDING SAS PROCESSING <ul><li>In order to understand how macro variables are processed and stored, one needs to understand how SAS processing works </li></ul>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. 12. UNDERSTANDING SAS PROCESSING <ul><li>When a program is submitted, all the codes are placed in a memory area, called the input stack </li></ul>data ex1; set height; tall = height > 63 ; run ; INPUT STACK
  13. 13. <ul><li>The word scanner takes the statements from the input stack and tokenizes the statements into tokens </li></ul><ul><li>The word scanner then directs the tokens to the right location </li></ul><ul><ul><li>DATA step compiler </li></ul></ul><ul><ul><li>Macro processor, etc. </li></ul></ul>UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK data ex1; set height; tall = height > 63 ; run ;
  14. 14. data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ex1; set height; tall = height > 63 ; run ;
  15. 15. ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; set height; tall = height > 63 ; run ;
  16. 16. ; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK set height; tall = height > 63 ; run ;
  17. 17. set ; ex1 data UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height; tall = height > 63 ; run ;
  18. 18. height set ; data ex1 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; tall = height > 63 ; run ;
  19. 19. ; height set data ex1; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK tall = height > 63 ; run ;
  20. 20. tall ; height data ex1; set UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK = height > 63 ; run ;
  21. 21. = tall ; data ex1; set height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK height > 63 ; run ;
  22. 22. height = tall data ex1; set height; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK > 63 ; run ;
  23. 23. > height = data ex1; set height; tall UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK 63 ; run ;
  24. 24. 63 > height data ex1; set height; tall = UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ; run ;
  25. 25. ; 63 > data ex1; set height; tall = height UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK run ;
  26. 26. run ; 63 data ex1; set height; tall = height > UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ;
  27. 27. ; run ; data ex1; set height; tall = height > 63 UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
  28. 28. ; run data ex1; set height; tall = height > 63 ; UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
  29. 29. ; data ex1; set height; tall = height > 63 ; run UNDERSTANDING SAS PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK
  30. 30. data ex1; set height; tall = height > 63 ; run ; <ul><li>When the compiler receives the semicolon following the RUN statement, it stops taking tokens from the word scanner </li></ul>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. 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. 32. <ul><li>The macro facility includes a macro processor that is responsible for processing all macro language elements </li></ul><ul><li>Macro variable references and %LET statements are part of the macro language </li></ul>MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK %let ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  33. 33. <ul><li>Macro trigger: </li></ul><ul><li>%LET name-token </li></ul><ul><li>& name-token </li></ul><ul><li>The word scanner needs to recognize the macro language and direct them to the macro processor </li></ul>MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK %let ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  34. 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. 35. let % <ul><li>When the word scanner detects %LET (a macro trigger), it starts to direct the tokens to the macro processor </li></ul>MACRO PROCESSING WORD SCANNER TOKENNIZER DETERMINE DESTINATION COMPILER INPUT STACK ht =63; data ex1; set height; tall =height >&ht; run ; MACRO PROCESSOR
  36. 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. 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. 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. 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. 40. ex1 data ; 63 <ul><li>The macro processor stops processing the tokens when the semicolon is encountered </li></ul>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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 50. ht & > data ex1; set height; tall = height <ul><li>When the word scanner encounters the ampersand followed by HT, it directs them to the macro processor </li></ul>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. 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. 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. 53. ht data ex1; set height; tall = Height > <ul><li>The macro processor looks up the macro variable HT and takes its value from the symbol take </li></ul>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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 64. PROCESSING MACRO VARIABLES AT EXECUTION TIME <ul><li>In many applications, we need to create macro variables during DATA step execution </li></ul><ul><li>For example, we might need to create macro variables and assign values to them based on </li></ul><ul><ul><li>data values in SAS data sets or in external files </li></ul></ul><ul><ul><li>programming logic </li></ul></ul><ul><ul><li>computed values </li></ul></ul>
  65. 65. PROCESSING MACRO VARIABLES AT EXECUTION TIME <ul><li>Create macro variable TITL - serves as a title for printing the dataset ex1 </li></ul><ul><ul><li>If there are some students > 63 inches (when tall = 1), </li></ul></ul><ul><ul><li>TITL = “Some students are taller than 63 inches” </li></ul></ul><ul><ul><li>If none of the students > than 63 inches, </li></ul></ul><ul><ul><li>TITL = “None of the students are taller than 63 inches” </li></ul></ul>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. 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 = &quot;Some students are taller than 63 inches&quot;; end ; else do ; %let titl = &quot;None of the students are taller than 63 inches&quot;; 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. 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. 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 =&quot;Some students are taller than 63 inches&quot;; end ; else do ; %let titl = &quot;None of the students are taller than 63 inches&quot;; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables
  69. 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 =&quot;Some students are taller than 63 inches&quot;; end ; else do ; %let titl = &quot;None of the students are taller than 63 inches&quot;; end ; end ; run ; MACRO PROCESSOR GLOBAL SYMBOL TABLE SAS Automatic Macro Variables SYSDATE 19JUN06 SYSDAT Monday SYSTIME 10:34 . . User-Defined Macro Variables
  70. 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 = &quot;None of the students are taller than 63 inches&quot;; 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. 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 = &quot;None of the students are taller than 63 inches&quot;; 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. 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 = &quot;None of the students are taller than 63 inches&quot;; 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. 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 = &quot;None of the students are taller than 63 inches&quot;; 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. 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. 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. 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 = &quot;Some students are taller than 63 inches&quot;; end ; else do ; %let titl = &quot;None of the students are taller than 63 inches&quot;; 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. 77. THE SYMPUT ROUTINE <ul><li>To create a macro variable during the DATA step execution, use the SYMPUT routine </li></ul>CALL SYMPUT (MACRO-VARIABLE, VALUE); <ul><li>Both MACRO-VARIABLE and VALUE can be specified as </li></ul><ul><ul><li>literal (text in quotations) </li></ul></ul><ul><ul><li>a DATA step variable </li></ul></ul><ul><ul><li>a DATA step expression </li></ul></ul>
  78. 78. THE SYMPUT ROUTINE CALL SYMPUT (MACRO-VARIABLE, VALUE); <ul><li>Both arguments are literals </li></ul>‘ 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. 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 ; <ul><li>Use the SUMPUT routine to fix the previous program </li></ul>
  80. 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. 81. THE SYMPUT ROUTINE CALL SYMPUT (MACRO-VARIABLE, VALUE); <ul><li>When VALUE is a DATA step variable </li></ul>A DATA step variable; not in quotation marks <ul><li>You are assigning the value of a DATA step variable to the MACRO-VARIABLE </li></ul><ul><li>Any leading or trailing blanks that are part of the values of a DATA step variable will be part of the macro variables </li></ul><ul><li>For a numeric variable, the values will be converted to character variables by using the BEST12. format </li></ul>
  82. 82. THE SYMPUT ROUTINE <ul><li>Suppose that you would like to create four macro variables: </li></ul>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. 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. 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. 85. THE SYMPUT ROUTINE CALL SYMPUT (MACRO-VARIABLE, VALUE); <ul><li>When MACRO-VARIABLE is a DATA step variable </li></ul>A DATA step variable; not in quotation marks <ul><li>You are creating multiple macro variables </li></ul><ul><li>The names of the macro variables are the values of a DATA step variable </li></ul>
  86. 86. THE SYMPUT ROUTINE <ul><li>Suppose that you would like to create four macro variables: </li></ul>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. 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. 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. 89. THE SYMPUT ROUTINE CALL SYMPUT (MACRO-VARIABLE, VALUE); <ul><li>DATA step expressions can be used in one/both arguments </li></ul>A DATA step expression A DATA step expression
  90. 90. THE SYMPUT ROUTINE <ul><li>Suppose that you would like to create four macro variables: </li></ul>Height: <ul><li>Also, remove the leading blanks </li></ul>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. 91. THE SYMPUT ROUTINE Height: data _null_ ; set height; call symput ( &quot;height_&quot; ||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. 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. 93. THE SYMPUTX ROUTINE <ul><li>The SYMPUTX routine is an improved version of the SYMPUT routine </li></ul><ul><li>The SYMPUTX routine can remove leading and trailing blanks from both arguments </li></ul><ul><li>For example, the previous program can be re-written as below: </li></ul>data _null_ ; set height; call symputx ( &quot;height_&quot; ||name, height); run ;
  94. 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. 95. THE SYMPUTX ROUTINE <ul><li>Difference between CALL SYMPUTX and CALL SYMPUT </li></ul>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. 96. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>You can create/update macro variables during the execution of PROC SQL </li></ul><ul><li>The INTO clause in the SELECT statement performs a similar role to the SYMPUT(X) routine in the DATA step </li></ul><ul><li>The INTO clause </li></ul><ul><ul><li>can create one or more macro variables </li></ul></ul><ul><ul><li>can assign a calculated result or the value of a data variable to a macro variable </li></ul></ul><ul><ul><li>can only be used in the outer query of a SELECT statement </li></ul></ul><ul><ul><li>cannot be used when you are creating a table or a view </li></ul></ul>
  97. 97. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Here is the general syntax for PROC SQL and the INTO clause: </li></ul>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. 98. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Here is the general syntax for PROC SQL and the INTO clause: </li></ul>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. 99. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Here is the general syntax for PROC SQL and the INTO clause: </li></ul>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. 100. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Here is the general syntax for PROC SQL and the INTO clause: </li></ul>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. 101. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Here is the general syntax for PROC SQL and the INTO clause: </li></ul>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. 102. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>You can assign a calculated summary statistic to a macro variable </li></ul><ul><li>Suppose you would like to create a macro variable MEAN_HT - average heights of students </li></ul>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. 103. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Without using the PROC SQL, you must use multiple steps to accomplish this task </li></ul>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. 104. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>In the previous program, we created four macro variables: </li></ul>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 ; <ul><li>We can use PROC SQL to achive the same task </li></ul>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. 105. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>You can create a range of macro variables </li></ul><ul><li>Each of the macro variables will contain each row in the result of the SELECT statement </li></ul>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. 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. 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 ; <ul><li>By default, the leading and trailing blanks are removed from values before they are stored in macro variables </li></ul><ul><li>If you don’t want to remove the leading and trailing blanks, you can use the NOTRIM option </li></ul>
  108. 108. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heights </li></ul>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. 109. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Suppose that you would like to create macro variables NAME1 – NAME4 and HEIGHT1 – HEIGHT4 to store these four students’ names and their heights </li></ul>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. 110. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>When creating a range of macro variables, PROC SQL restricts the name convention for the macro variables </li></ul><ul><li>The names of the macro variables must end with an integer with a valid range, such as </li></ul><ul><li>HEIGHT1 – HEIGHT4 </li></ul><ul><li>You won’t be able to use more meaningful names such as JOHN_HT, TOM_HT, MARY_HT, and HELEN_HT </li></ul>
  111. 111. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>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 </li></ul>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. 112. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>Create macro variables </li></ul>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. 113. PROCESSING MACRO VARIABLES DURING THE EXECUTION OF PROC SQL <ul><li>proc sql noprint ; </li></ul><ul><li>select name, height </li></ul><ul><ul><li>into : namelist separated by ' ' , </li></ul></ul><ul><ul><li> : heightlist separated by ',' </li></ul></ul><ul><li>from height; </li></ul><ul><li>quit ; </li></ul><ul><li>%put macro variable namelist: &namelist; </li></ul><ul><li>%put macro variable heightlist: &heightlist; </li></ul>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. 114. CONCLUSION <ul><li>Creating a macro variable by using the %LET statement occurs before the execution of any other SAS language statements </li></ul><ul><li>To create a macro variable during the DATA step execution, you must use either the SYMPUT or SYMPUTX routines </li></ul><ul><li>To create a macro variable during the execution of PROC SQL, you must use the INTO clause </li></ul><ul><li>Understanding the mechanisms of creating macro variables is an important foundation for learning how best to write macro programs </li></ul>
  115. 115. REFERENCES <ul><li>Burlew, Michele M. SAS ® Macro Programming Made Easy, 2 nd Edition. </li></ul><ul><li>SAS Online Doc ® 9.1.3. Cary, NC: SAS Institute Inc. </li></ul>
  116. 116. ACKNOWLEDGEMENT <ul><li>I would like to thank Kathryn McLawhorn and Scott McElroy, Technical Support Analysts from SAS Technical Support, for their valuable programming suggestions and insight </li></ul>
  117. 117. CONTACT INFORMATION <ul><li>Arthur Li </li></ul><ul><li>City of Hope </li></ul><ul><li>Division of Information Science </li></ul><ul><li>1500 East Duarte Road </li></ul><ul><li>Duarte, CA 91010 - 3000 </li></ul><ul><li>Phone: (626) 256-4673 ext. 65121 </li></ul><ul><li>E-mail: xueli@coh.org </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×