This document summarizes key concepts about macro programs in SAS including:
1) The basic structure of macro programs including defining macros with %MACRO and %MEND statements and compiling macros.
2) How macro compilation and execution works including resolving macro variables and inserting text into the input stack.
3) How to define macros with parameter lists including positional and keyword parameters and calling macros with different parameter values.
4) Tips for writing macro-based programs such as debugging with MPRINT and SYMBOLGEN options.
12. Submitting a macro definition compiles the macro. After the macro compiles successfully, you execute it with a macro call. General form of the macro call: %macro-name Calling a Macro Macro Program
13.
14. Example: Define a simple macro that produces code to print the first five observations from the most recently created SAS data set. %macro prtlast; proc print data=&syslast(obs=5); title "Listing of &syslast data set"; run; %mend; Calling a Macro Macro Program
15. Suppose a program consists of several program steps that create SAS data sets. If the macro PRTLAST is compiled, you can reference it after each step. Calling a Macro Macro Program
16. proc sort data=perm.courses out=courses; by course_code; run; %prtlast proc sort data=perm.schedule out=schedule; by begin_date; run; %prtlast proc sort data=perm.students out=students; by student_name; run; %prtlast Calling a Macro Macro Program
17.
18. The macro facility has reserved words that cannot be used as macro names: ABEND ABORT ACT ACTIVATE BQUOTE BY CLEAR CLOSE CMS COMANDR COPY DEACT DEL DELETE DISPLAY DMIDSPLY DMISPLIT DO EDIT ELSE END EVAL FILE GLOBAL GO GOTO IF INC INCLUDE INDEX INFILE INPUT KEYDEF LENGTH LET LIST continued... Macro Program
19. LISTM LOCAL MACRO MEND METASYM NRBQUOTE NRQUOTE NRSTR ON OPEN PAUSE PUT QSCAN QSUBSTR QSYSFUNC QUOTE QUPCASE RESOLVE RETURN RUN SAVE SCAN STOP STR SUBSTR SUPERQ SYSCALL SYSEVALF SYSEXEC SYSFUNC SYSGET SYSRPUT THEN TO TSO UNQUOTE UNSTR UNTIL UPCASE WHILE WINDOW Reserved Words in the Macro Facility Macro Program
20. Example: Attempt to define a macro named PUT. SAS Log What ambiguity would result if you could define a macro named PUT? Reserved Words in the Macro Facility Macro Program
21.
22.
23. Macro Execution During macro execution, the macro processor can communicate directly with: symbol tables storing values with %LET and resolving macro variable references (&macvar ). the input stack generating SAS code for tokenization by the word scanner. During macro execution, the macro processor cannot communicate directly with SAS data sets to obtain or modify values of variables. Macro Execution Macro Program
24.
25.
26. %macro prtlast; proc print data=&syslast(obs=5); title "Listing of &syslast data set"; run; %mend; WORK.SASMACR.PRTLAST.MACRO Catalog Entry Original Input Stack %prtlast Macro Execution Macro Program
27. proc print data=&syslast(obs=5); title "Listing of &syslast data set"; run; Input Stack after Substitution of Text Partial SAS Log Why do you see a note from PROC PRINT, but not the PROC PRINT code itself? Macro Execution Macro Program
28.
29. The MPRINT option prints the text sent to the SAS compiler as a result of macro execution. General form of the MPRINT|NOMPRINT option: OPTIONS MPRINT; OPTIONS NOMPRINT; The default setting is NOMPRINT . Monitoring Execution Macro Program
30. The MLOGIC prints messages indicating macro actions taken during macro execution. General form of the MLOGIC|NOMLOGIC option: OPTIONS MLOGIC; OPTIONS NOMLOGIC; The default setting is NOMLOGIC. Monitoring Execution Macro Program
31.
32.
33. SAS Log The macro variable reference is resolved. The MPRINT System Option Macro Program
34.
35. SAS Log Specify the MPRINT, MLOGIC, and SYMBOLGEN system options to obtain all possible information from the macro processor. The MLOGIC System Option Macro Program
36. Example: Successfully submit a macro definition and print a report showing where compiled macros are stored by default. %macro prtlast; proc print data=&syslast(obs=5); title "Listing of &sysast data set"; run; %mend; proc catalog cat=work.sasmacr; contents; title "Default Storage of SAS Macros"; quit; Macro Program
37. Partial PROC CATALOG Output The list of entries in a SAS catalog can also be obtained through the Catalog window. Macro Storage (Optional) Macro Program
38. Comments in Macro Programs (Optional) You can place comments within a macro definition with the macro comment statement. General form of the macro comment statement: %* comment ; comment can be any message. Macro Program
39. Example: Use macro comments to describe the functionality of a macro. %macro printit; %* The value of &syslast will be substituted appropriately ; %* as long as a data set has been created in this session. ; proc print data=&syslast(obs=5); /* Only print the first 5 observations */ title "Last Created Data Set Is &syslast"; run; %mend; Comments in Macro Programs (Optional) Macro Program
42. Suppose the compiled macro PRINTDSN contains a reference to macro variables DSN and VARS. %macro printdsn; proc print data=&dsn; var &vars; title "Listing of %upcase(&dsn) data set"; run; %mend; You can modify the behavior of PRINTDSN by changing the value of macro variable DSN or VARS before you call the macro. Macro Parameters
43. Example: Substitute perm.courses for DSN and course_code course_title days for VARS at macro execution. %let dsn=perm.courses; %let vars=course_code course_title days; %printdsn Example: Substitute perm.schedule for DSN and course_code location begin_date for VARS at macro execution. %let dsn=perm.schedule; %let vars=course_code location begin_date; %printdsn Introduction Macro Parameters
44. A revised definition of the PRINTDSN macro has a parameter list in the %MACRO statement. This list contains names of macro variables referenced only within the macro. %macro printdsn(dsn,vars); proc print data=&dsn; var &vars; title "Listing of %upcase(&dsn) data set"; run; %mend; Macro Parameters
45.
46. To call a macro with parameters, specify the values to be assigned to the parameters in the macro call. General form of a macro call with parameters: %macro-name(parameter-values) Macro Parameter Lists Macro Parameters
47.
48. To call the PRINTDSN macro and provide values for parameters DSN and VARS, you submit the following code: It substitutes perm.courses for DSN and course_code course_title days for VARS during macro execution. %printdsn(perm.courses, course_code course_title days) Macro Parameter Lists Macro Parameters
49. You can later submit the following code: This code substitutes perm.schedule for DSN and course_code location begin_date for VARS during macro execution. %printdsn(perm.schedule, course_title location begin_date) Macro Parameter Lists Macro Parameters
50. Variables declared in a parameter list are always stored in a separate symbol table known as a local tabl e. The macro call %printdsn(perm.courses,course_code course_title days) initializes a local table: Global Table Local Table global variables DSN perm.courses VARS course_code course_title days Macro Parameters
51.
52. You can define macros with positional parameters. General form of a macro definition with positional parameters: %MACRO macro-name(positional-1, ...,positional-n ); text referencing parameter variables %MEND ; Macro Parameters
53.
54.
55.
56. Macros with Keyword Parameters You can also define macros with keyword parameters. General form of a macro definition with keyword parameters : %MACRO macro-name(keyword=value, ...,keyword=value ); text referencing parameter variables %MEND ; Macro Parameters
57.
58. General form of a call to a macro defined with keyword parameters : %macro-name(keyword=value, ...,keyword=value) Macros with Keyword Parameters Macro Parameters
59.
60.
61. You can also define macros with a parameter list containing both positional and keyword parameters. General form of a macro definition with mixed parameters: %MACRO macro-name(positional-1, ...,positional-n , keyword=value, ...,keyword=value ); text referencing parameter variables %MEND ; All positional parameter variables in the %MACRO statement must be listed before any keyword parameter variable. Macros with Mixed Parameter Lists Macro Parameters
62. General form of a call to a macro defined with mixed parameters: %macro-name(value-1, ...,value-n , keyword=value, ...,keyword=valu e) Positional values must be listed before any keyword parameter in the macro call. Macros with Mixed Parameter Lists Macro Parameters
63.
64. For macros defined with a parameter list, the token after %macro-name triggers macro execution. Therefore, to use all default parameter values, use the call %attend() . The call %attend will not execute until another token is encountered. Caution Macros with Keyword Parameters Macro Parameters