Defining and Calling a Macro
A macro definition allows you to write custom macro programs in SAS. Learn about defining macros, compiling them, and storing them in the temporary catalog. Discover how to call macros in your SAS programs.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Defining a Macro A macro or macro definition enables you to write macro programs. %MACROmacro-name; macro-text %MEND <macro-name>; macro-name macro-text Any combination of: text SAS statements macro variable references macro statements, expressions, or calls follows SAS naming conventions. can include : 2
Macro Compilation When a macro definition is submitted: 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. 3
Macro Compilation The MCOMPILENOTE=ALL option issues a note to the SAS log after a macro definition has compiled. OPTIONS MCOMPILENOTE=ALL|NONE; The default setting is MCOMPILENOTE=NONE. 4
Macro Compilation %macro %macro time %put The current time is %sysfunc(time(),timeampm.).; %mend %mend time; time; 5
Macro Compilation options mcompilenote=all; %macro %macro time time; %put The current time is %sysfunc(time(),timeampm.).; %mend %mend time; 6
Macro Storage Example: Produce a list of compiled macros stored in the default temporary catalog work.sasmacr. proc proc catalog catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit quit; title; 7
Calling a Macro 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). %macro-name 8
%time time 9
Simple Macro A macro often generates SAS code. %macro %macro calc proc means data=mac1.order_item &stats; var &vars; run; %mend %mend calc; calc; proc proc catalog catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit quit; title; This macro contains no macro language statements. 10
Simple Macro Example: Call the CALC macro. Precede the call with %LET statements that create the macro variables referenced within the macro. %let stats=min max; %let vars=quantity; %calc calc 11
Macro Execution The MPRINT option writes to the SAS log 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. 12
Program Flow, When the macro processor receives %macro-name 1. Searches the designated SAS catalog (work.sasmacr by default) for an entry named macro-name.MACRO 2. Executes compiled macro language statements, if any 3. Sends other text to the input stack for word scanning 4. Pauses while the word scanner tokenizes inserted text, and SAS code, if any, compiles and executes 5. Resumes execution of macro language statements after SAS code executes 13
Program Flow Compiler Symbol Table Word Scanner Macro Processor work.sasmacr # Name 1 CALC 2 TIME Input Stack %let stats=min max; %let vars=quantity; %calc Type MACRO MACRO 14 ...
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 %calc work.sasmacr # Name 1 CALC 2 TIME Type MACRO MACRO 15 ...
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 1 CALC 2 TIME Type MACRO MACRO 16 ...
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; 17 ...
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 proc means data=orion.order_item &stats; var &vars; run; CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; 18 ...
Program Flow Macro activity pauses while the word scanner tokenizes text and passes it to the compiler. Compiler proc means data=orion.order_item Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack &stats; var &vars; run; CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; 19 ...
Program Flow Macro variable references are passed to the macro processor. Compiler proc means data=orion.order_item Symbol Table 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; 20 ...
Program Flow Symbolic substitution is performed. Compiler proc means data=orion.order_item Symbol Table 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; min max; var &vars; run; 21 ...
Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler proc means data=orion.order_item min max; 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; var &vars; run; 22 ...
Program Flow Word scanning continues. Compiler proc means data=orion.order_item min max; var 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; &vars; run; 23 ...
Program Flow Macro variable references are passed to the macro processor. Compiler proc means data=orion.order_item min max; var Symbol Table STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ; run; 24 ...
Program Flow Symbolic substitution is performed. Compiler proc means data=orion.order_item min max; var Symbol Table STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; run; quantity; 25 ...
Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler proc means data=orion.order_item min max; var quantity; 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; run; 26 ...
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 proc means data=orion.order_item min max; var quantity; Symbol Table STATS min max VARS quantity Word Scanner run; Macro Processor Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; 27
Macro Execution The SAS log reports 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. 28
Macro Execution The MPRINT option writes to the SAS log 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. 29
Macro Execution options mprint; %calc calc options nomprint; 30