1.6k likes | 1.63k Views
Chapter 2: Macro Variables. Chapter 2: Macro Variables. Objectives. Describe what macro variables store and where macro variables are stored. Identify the two types of macro variables. Macro Variables.
E N D
Objectives • Describe what macro variables store and where macro variables are stored. • Identify the two types of macro variables.
Macro Variables • Macro variables are called symbolic variables because SAS programs can reference macro variables as symbols for SAS code. • Macro variables store text, including the following: • complete or partial SAS steps • complete or partial SAS statements
Global Symbol Table Global Symbol Table SYSDATE 09NOV07 SYSDATE9 09NOV2007 SYSDAY Friday SYSTIME 10:47 SYSUSERID joeuser . . . . Automatic Variables Macro variables are stored in a memory area called the globalsymbol table. When SAS is invoked, the global symbol table is created and initialized with automatic macro variables.
Global Symbol Table • User-defined macro variables can be added to the global symbol table. Global Symbol Table . . . . SYSTIME 10:47 SYSUSERID joeuser . . . . OFFICE Sydney DATE1 25may2008 UNITS 4 Automatic Variables User-Defined Variables
Macro Variables • Macro variables in the global symbol table • are global in scope (always available) • have a minimum length of 0 characters (nullvalue) • have a maximum length of 65,534 (64K) characters • store numeric tokens as text.
2.01 Quiz What are the two kinds of macro variables? Where are macro variables stored?
2.01 Quiz – Correct Answers What are the two kinds of macro variables? Automatic and user-defined Where are macro variables stored? In the global symbol table
Objectives • Identify selected automatic macro variables. • Display automatic macro variables in the SAS log.
Automatic Macro Variables • The following are true for automatic macro variables: • system-defined • created at SAS invocation • global in scope (always available) • assigned values by SAS • can be assigned values by the user in some cases
System-Defined Automatic Macro Variables Some automatic macro variables have fixed values that are set at SAS invocation:
System-Defined Automatic Macro Variables Some automatic macro variables have values that change automatically based on submitted SAS statements:
Automatic Macro Variables %put _automatic_; Example: Write the names and values of all automatic macro variables to the SAS log using the _AUTOMATIC_ argument of the %PUT statement.
Automatic Macro Variables 12 %put _automatic_; AUTOMATIC AFDSID 0 AUTOMATIC AFDSNAME AUTOMATIC AFLIB AUTOMATIC AFSTR1 AUTOMATIC AFSTR2 AUTOMATIC FSPBDV AUTOMATIC SYSBUFFR AUTOMATIC SYSCC 3000 AUTOMATIC SYSCHARWIDTH 1 AUTOMATIC SYSCMD AUTOMATIC SYSDATE 05FEB08 AUTOMATIC SYSDATE9 05FEB2008 Partial SAS Log • The macro variables SYSDATE, SYSDATE9, and SYSTIME store character strings, not SAS date or time values.
2.02 Quiz %put _automatic_; Submit the following statement: What is the value of SYSSCPL?
2.02 Quiz – Correct Answer %put _automatic_; Submit the following statement: What is the value of SYSSCPL? SYSSCPL is the name of the operating system being used. The value will vary. For example, the value could be XP_PRO in a Windows environment.
Objectives • Explain how macro variable references are handled by the word scanner and macro processor.
Macro Variable References • The following are true for macro variable references: • begin with an ampersand (&) followed by a macro variable name • can appear anywhere in your program • are not case sensitive • are also called symbolic references • represent macro triggers • are passed to the macro processor • When the macro processor receives a macro variable reference, it does the following: • searches the symbol table for the macro variable • resolves the macro variable by substituting its value • issues a warning to the SAS log if the macro variable is not found in the symbol table
Macro Variable References Example: Write the day of the week to the SAS log. Partial SAS Log 12 %put Today is &sysday; Today is Tuesday
Substitution within a Macro Statement Compiler Macro Processor WordScanner Symbol Table %put Today is &sysday; InputStack SYSDAY SYSLAST Tuesday _NULL_ ...
Substitution within a Macro Statement When a macro trigger is encountered, it is passed to the macro processor for evaluation. Compiler Macro Processor WordScanner %put Symbol Table Today is &sysday; InputStack SYSDAY SYSLAST Tuesday _NULL_ ...
Substitution within a Macro Statement • The macro processor requests tokens until a semicolon is encountered. Compiler Macro Processor ; WordScanner %put Today is &sysday Symbol Table InputStack SYSDAY SYSLAST Tuesday _NULL_ ...
Substitution within a Macro Statement • The macro variable referencetriggers the macro processor to search the symbol table for the reference. Compiler Macro Processor ; WordScanner %put Today is &sysday Symbol Table InputStack SYSDAY SYSLAST Tuesday _NULL_ ...
Substitution within a Macro Statement • The macro processor resolves the macro variable reference, substituting its value. Compiler Macro Processor ; WordScanner %put Today is Tuesday Symbol Table InputStack Tuesday _NULL_ SYSDAY SYSLAST ...
Substitution within a Macro Statement The macro processor executes the %PUT statement, writing the resolved text to the SAS log. Compiler Macro Processor WordScanner %put Today is Tuesday; Symbol Table InputStack Tuesday _NULL_ SYSDAY SYSLAST
2.03 Quiz proc freq data=orion.Customer; table Country / nocum; footnote1 'Created &systime &sysday, &sysdate9'; footnote2 'By user &sysuserid on system &sysscpl'; run; Submit program m102d01a. What are the footnotes in the PROC FREQ output?
2.03 Quiz – Correct Answer Submit program m102d01a. What are the footnotes in the PROC FREQ output? Created &systime &sysday, &sysdate9 By user &sysuserid on system &sysscpl proc freq data=orion.Customer; table Country / nocum; footnote1 'Created &systime &sysday, &sysdate9'; footnote2 'By user &sysuserid on system &sysscpl'; run;
Substitution within a SAS Literal SAS Output Customer Country Country Frequency Percent ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ AU 8 10.39 CA 15 19.48 DE 10 12.99 IL 5 6.49 TR 7 9.09 US 28 36.36 ZA 4 5.19 Created &systime &sysday, &sysdate9 By user &sysuserid on system &sysscpl The word scanner does not tokenize literals enclosed in single quotation marks, so macro variables do not resolve.
Substitution within a SAS Literal Example: Substitute system information in footnotes. proc freq data=orion.Customer; table Country / nocum; footnote1 "Created &systime &sysday, &sysdate9"; footnote2 "By user &sysuserid on system &sysscpl"; run; To reference macro variables within a literal, enclose the literal in double quotation marks. m102d01b
Substitution within a SAS Literal • PROC FREQ Output Customer Country Country Frequency Percent ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ AU 8 10.39 CA 15 19.48 DE 10 12.99 IL 5 6.49 TR 7 9.09 US 28 36.36 ZA 4 5.19 Created 10:47 Friday, 09NOV2007 By user joeuser on system XP_PRO
Substitution within a SAS Literal Compiler Macro Processor WordScanner Symbol Table InputStack proc print; title "Today is &sysday"; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • SAS statements are passed to the compiler. proc print; title Compiler Macro Processor WordScanner " Today is Symbol Table InputStack &sysday"; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • The macro trigger is passed to the macro processor. proc print; title Compiler Macro Processor &sysday WordScanner " Today is Symbol Table InputStack "; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • The macro processor searches the symbol table. proc print; title Compiler Macro Processor &sysday WordScanner " Today is Symbol Table InputStack "; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • The resolved reference is passed back to the input stack. proc print; title Compiler Macro Processor WordScanner " Today is Symbol Table InputStack Tuesday"; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • Word scanning continues. proc print; title Compiler Macro Processor WordScanner " Today is Tuesday " Symbol Table InputStack ; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • The double-quoted literal is passed to the compiler as a unit. proc print; title "Today is Tuesday" Compiler Macro Processor WordScanner Symbol Table InputStack ; run; SYSDAY SYSLAST Tuesday WORK.ALL ...
Substitution within a SAS Literal • When a step boundary is encountered, compilation ends and execution begins. proc print; title "Today is Tuesday"; Compiler Macro Processor WordScanner run; Symbol Table InputStack SYSDAY SYSLAST Tuesday WORK.ALL
2.04 Multiple Choice Poll • Macro variable references are resolved by which of the following? • SAS compiler • Macro processor • Word scanner
2.04 Multiple Choice Poll – Correct Answer • Macro variable references are resolved by which of the following? • SAS compiler • Macro processor • Word scanner
Substitution within SAS Code Example: Generalize PROC PRINT to print the last created data set, using the automatic macro variable SYSLAST. Compiler Macro Processor WordScanner Symbol Table proc print data=&syslast; title "Listing of &syslast"; run; InputStack SYSDAY SYSLAST Tuesday ORION.ALL ...