Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining and Calling a Macro

Similar presentations


Presentation on theme: "Defining and Calling a Macro"— Presentation transcript:

1 Defining and Calling a Macro
3 Defining and Calling a Macro

2 Defining a Macro A macro or macro definition enables you to write macro programs. macro-name follows SAS naming conventions. macro-text can include : Any combination of: text SAS statements macro variable references macro statements, expressions, or calls %MACRO macro-name; macro-text %MEND <macro-name>; This slide says "A macro or macro definition enables you to write macro programs", but we don't write any macro programs in this chapter, unless you count a single %put statement as a program, so students must wait till chapter 5 to see macro programs. In this chapter, we lay the groundwork for chapter 5. A macro, or macro definition, is the container that holds a macro program. We'll learn about that container in this chapter, and we'll fill in the container with macro programs in chapter 5.

3 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.sasmacr by default. There are only two fundamental things you can put in a macro: 1) macro language statements; and 2) anything else. Anything else is text. SAScode is text. The macro processor knows nothing about SAScode. It only knows about stuff that begins with a percent sign or an ampersand. Just as a macro variable must be created before it can be referenced, a macro must be created, or defined, before it can be called.

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

5 Macro Compilation %macro time;
%put The current time is %sysfunc(time(),timeampm.).; %mend time; This is a simple macro program because it contains a macro language statement. It is the only macro program we will see in this chapter.

6 Macro Compilation options mcompilenote=all; %macro time;
%put The current time is %sysfunc(time(),timeampm.).; %mend time; This is a simple macro program because it contains a macro language statement. It is the only macro program we will see in this chapter.

7 Macro Storage Example: Produce a list of compiled macros stored in the default temporary catalog work.sasmacr. proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; title;

8 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). Having previously defined a macro, we can call it. You call a macro the same way you call your dog. By name. %macro-name

9 %time Calling the time macro causes the %put statement to execute.

10 Simple Macro A macro often generates SAS code.
%macro calc; proc means data=mac1.order_item &stats; var &vars; run; %mend calc; proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; title; Strictly speaking, this macro does not contain a macro program, because there are no macro language statements. The macro contains only text, including two macro variable references. When the macro definition is submitted, there is nothing for the macro processor to compile. This macro simply stores the text, much as you would store a text file. This is not a good use of a macro, although people do this sort of thing all the time. This macro contains no macro language statements.

11 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

12 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: The default setting is NOMPRINT. OPTIONS MPRINT; OPTIONS NOMPRINT;

13 Program Flow, When the macro processor receives %macro-name
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 Here again, a macro stores only two things: 1) macro language statements; or 2) text. When you call a macro, the rule is simple: compiled macro language statements are executed. Anything else is sent to the input stack.

14 Program Flow work.sasmacr Compiler Symbol Table Word Scanner
Macro Processor Input Stack work.sasmacr %let stats=min max; %let vars=quantity; %calc # Name Type 1 CALC MACRO 2 TIME MACRO ...

15 Program Flow work.sasmacr
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 ...

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

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

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

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

20 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 ; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

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

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

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

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

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

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

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

28 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 seconds cpu time seconds PROC MEANS source code does not appear in the SAS log.

29 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: The default setting is NOMPRINT. OPTIONS MPRINT; OPTIONS NOMPRINT;

30 Macro Execution options mprint; %calc options nomprint;


Download ppt "Defining and Calling a Macro"

Similar presentations


Ads by Google