1 / 160

Chapter 2: Macro Variables

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.

sammiep
Download Presentation

Chapter 2: Macro Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Macro Variables

  2. Chapter 2: Macro Variables

  3. Objectives • Describe what macro variables store and where macro variables are stored. • Identify the two types of macro variables.

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

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

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

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

  8. 2.01 Quiz What are the two kinds of macro variables? Where are macro variables stored?

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

  10. Chapter 2: Macro Variables

  11. Objectives • Identify selected automatic macro variables. • Display automatic macro variables in the SAS log.

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

  13. System-Defined Automatic Macro Variables Some automatic macro variables have fixed values that are set at SAS invocation:

  14. System-Defined Automatic Macro Variables Some automatic macro variables have values that change automatically based on submitted SAS statements:

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

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

  17. 2.02 Quiz %put _automatic_; Submit the following statement: What is the value of SYSSCPL?

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

  19. Chapter 2: Macro Variables

  20. Objectives • Explain how macro variable references are handled by the word scanner and macro processor.

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

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

  23. Substitution within a Macro Statement Compiler Macro Processor WordScanner Symbol Table %put Today is &sysday; InputStack SYSDAY SYSLAST Tuesday _NULL_ ...

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

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

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

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

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

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

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

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

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

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

  34. Substitution within a SAS Literal Compiler Macro Processor WordScanner Symbol Table InputStack proc print; title "Today is &sysday"; run; SYSDAY SYSLAST Tuesday WORK.ALL ...

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

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

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

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

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

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

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

  42. 2.04 Multiple Choice Poll • Macro variable references are resolved by which of the following? • SAS compiler • Macro processor • Word scanner

  43. 2.04 Multiple Choice Poll – Correct Answer • Macro variable references are resolved by which of the following? • SAS compiler • Macro processor • Word scanner

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

More Related