1 / 90

Chapter 3: Macro Definitions

Chapter 3: Macro Definitions. Chapter 3: Macro Definitions. Objectives. Define and call a simple macro. Defining a Macro. A macro or macro definition enables you to write macro programs. General form of a macro definition: macro-name follows SAS naming conventions.

hou
Download Presentation

Chapter 3: Macro Definitions

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 3: Macro Definitions

  2. Chapter 3: Macro Definitions

  3. Objectives • Define and call a simple macro.

  4. Defining a Macro • A macro or macro definition enables you to write macro programs. • General form of a macro definition: • macro-name follows SAS naming conventions. • macro-text can include the following: %MACRO macro-name; macro-text %MEND <macro-name>; • any text • SAS statements or steps • macro variable references • macro statements, expressions, or calls • any combination of the above

  5. Macro Compilation • When a macro definition is submitted, the following occur: • Macro language statements, if any, are • checked for syntax errors • compiled. • SAS statements and other text are • not checked for syntax errors • not compiled. • The macro is stored as a SAS catalog entry in the temporary catalog work.sasmacrby default.

  6. Macro Compilation OPTIONS MCOMPILENOTE=ALL|NONE; The MCOMPILENOTE=ALL option issues a note to the SAS log after a macro definition has compiled. General form of the MCOMPILENOTE= option: The default setting is MCOMPILENOTE=NONE.

  7. Macro Compilation options mcompilenote=all; %macro time; %put The current time is %sysfunc (time(),timeampm.).; %mend time; 1 options mcompilenote=all; 2 %macro time; 3 %put The current time is %sysfunc 4 (time(),timeampm.).; 5 %mend time; NOTE: The macro TIME completed compilation without errors. 3 instructions 76 bytes. m103d01 Example: Submit a macro definition. SAS Log

  8. Macro Storage proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; PROC CATALOG Output My Temporary Macros Contents of Catalog WORK.SASMACR # Name Type Create Date Modified Date Description ---------------------------------------------------------------- 1 TIME MACRO 11JUN2004:15:55:59 11JUN2004:15:55:59 m103d02 Example: Produce a list of compiled macros stored in the default temporary catalog work.sasmacr.

  9. Calling a Macro %macro-name • A macro call • causes the macro to execute • is specified by placing a percent sign before the name of the macro • can be made anywhere in a program (similar to a macro variable reference) • represents a macro trigger • is not a statement (no semicolon required). • General form of a macro call:

  10. Calling a Macro %time 178 %time The current time is 2:49:39 PM. m103d01 Example: Call the TIME macro. SAS Log

  11. 3.01 Poll • Does the macro call below require a semicolon? • %time •  Yes • No

  12. 3.01 Poll – Correct Answer Does the macro call below require a semicolon? %time  Yes  No A macro call is not a statement. A semicolon is not required and can cause problems.

  13. Simple Macro %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; This macro contains no macro language statements. m103d03 A macro can generate SAS code. Example: Write a macro that generates a PROC MEANS step. Reference macro variables within the macro.

  14. Simple Macro %let stats=min max; %let vars=quantity; %calc m103d03 Example: Call the CALC macro. Precede the call with %LET statements that populate macro variables referenced within the macro.

  15. Program Flow • searches the designated SAS catalog (work.sasmacr by default) for an entry named macro-name.MACRO • executes compiled macro language statements, if any • sends other text to the input stack for word scanning • pauses while the word scanner tokenizes inserted text, and SAS code, if any, compiles and executes • resumes execution of macro language statements after SAS code executes When the macro processor receives %macro-name, it does the following:

  16. Program Flow Example: Submit the %LET statements and call the CALC macro. Compiler Symbol Table Word Scanner Macro Processor work.sasmacr Input Stack %let stats=min max; %let vars=quantity; %calc # Name Type 1 CALC MACRO 2 TIME MACRO ...

  17. Program Flow The macro processor executes the %LET statements and populates the symbol table. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack work.sasmacr %calc # Name Type 1 CALC MACRO 2 TIME MACRO ...

  18. Program Flow • When the macro processor receives %CALC, it locates CALC.MACRO within the work.sasmacrcatalog. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor %calc Input Stack work.sasmacr # Name Type 1CALC MACRO 2 TIME MACRO ...

  19. Program Flow • The macro processor opens CALC.MACRO. There are no macro language statements to execute. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  20. Program Flow • The macro processor places the macro text on the input stack. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO proc means data=orion.order_item &stats; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  21. Program Flow • Macro activity pauses while the word scanner tokenizes text and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO &stats; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  22. Program Flow Macro variable references are passed to the macro processor. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor &stats Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ; var &vars; run; ...

  23. Program Flow Symbolic substitution is performed. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor &stats Input Stack CALC.MACRO min max; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  24. Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item min max; STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  25. Program Flow Word scanning continues. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  26. Program Flow Macro variable references are passed to the macro processor. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO ; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  27. Program Flow Symbolic substitution is performed. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO quantity; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  28. Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item min max; var quantity; STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

  29. Program Flow When a step boundary is encountered, SAS executes the compiled step as macro activity remains paused. Macro activity stops when the %MEND statement is encountered. Compiler Symbol Table proc means data=orion.order_item min max; var quantity; STATS min max VARS quantity Word Scanner Macro Processor run; Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc;

  30. Macro Execution The SAS log reflects execution of the PROC MEANS step. 52 %let stats=min max; 53 %let vars=quantity; 54 %calc NOTE: There were 732 observations read from the data set ORION.ORDER_ITEM. NOTE: PROCEDURE MEANS used (Total process time): real time 0.03 seconds cpu time 0.03 seconds PROC MEANS source code does not appear in the SAS log. m103d03 SAS Log

  31. Macro Execution OPTIONS MPRINT; OPTIONS NOMPRINT; The MPRINT option writes to the SAS log the text that is generated by macro execution. General form of the MPRINT|NOMPRINT option: The default setting is NOMPRINT.

  32. Macro Execution 55 options mprint; 56 %calc MPRINT(CALC): proc means data=orion.order_item min max; MPRINT(CALC): var quantity; MPRINT(CALC): run; NOTE: There were 732 observations read from the data set ORION.ORDER_ITEM. NOTE: PROCEDURE MEANS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds Example: Set the MPRINT option before calling the macro. • Partial SAS Log

  33. Exercise This exercise reinforces the concepts discussed previously.

  34. Chapter 3: Macro Definitions

  35. Objectives • Define and call macros with parameters. • Describe the difference between positional parameters and keyword parameters.

  36. Review %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; m103d03 Example: Note macro variable references within the CALC macro.

  37. Review %let stats=min max; %let vars=quantity; %calc %let stats=n mean; %let vars=discount; %calc The user must submit three lines each time. How can this be simplified? m103d03 Example: Call the macro twice, each time with different values of the macro variables STATS and VARS.

  38. Macro Parameters %macro calc(stats,vars); proc means data=orion.order_item &stats; var &vars; run; %mend calc; m103d05 Example: Define a macro with a parameter list of macro variables referenced within the macro.

  39. Positional Parameters %macro calc(stats,vars); proc means data=orion.order_item &stats; var &vars; run; %mend calc; %calc(min max,quantity) m103d05 • Positional parameters use a one-to-one correspondence between the following: • parameter names supplied on the macro definition • parameter values supplied on the macro call

  40. Positional Parameters %MACRO macro-name(parameter-1, … parameter-n); macro text %MEND <macro-name>; • General form of a macro definition with positional parameters: • Parameter names are • parenthesized • comma delimited.

  41. Macro Parameters %macro-name(value-1, … value-n) • General form of a macro call with parameters: Parameter values are • parenthesized • comma delimited. Parameter values can be any text, null values, macro variable references, or macro calls.

  42. Local Symbol Tables When a macro with a parameter list is called, the parameters are created in a separate local symboltable. The macro call initializes a local table: %calc(min max, quantity) Local Table Global Table STATS min max VARS quantity SYSDAY Tuesday SYSLAST _NULL_ CITY Dallas AMOUNT 975

  43. Local Symbol Tables • A local symbol table is • created when a macro with a parameter list is called • deleted when the macro finishes execution. • Macro variables in the local table are available only during macro execution and can be referenced only within the macro.

  44. 3.02 Multiple Choice Poll • A %LET statement outside a macro definition creates a macro variable in the • global symbol table • local symbol table

  45. 3.02 Multiple Choice Poll – Correct Answer • A %LET statement outside a macro definition creates a macro variable in the • global symbol table • local symbol table

  46. Positional Parameters %macro count(opts, start, stop); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(nocum,01jan2004,31dec2004) %count(,01jul2004,31dec2004) m103d06a Example: Define and call a macro with positional parameters.

  47. Macros with Positional Parametersm103d06a This demonstration illustrates using positional parameters to specify a range of dates and TABLE statement options for the FREQ procedure.

More Related