Defining and Calling a Macro

A macro definition stores text. This might include macro language statements or expressions as well as complete or partial SAS program statements or program steps. The macro definition begins with a %MACRO statement and ends with a %MEND statement.

Defining a Macro

%MACRO and %MEND Statements Syntax

In order to create a macro program, you must first define it. You begin a macro definition with a %MACRO statement, and you end the definition with a %MEND statement.
Syntax, %MACRO statement and %MEND statement:
%MACRO macro-name;
text
%MEND <macro-name>;
macro-name
names the macro. The value of macro-name can be any valid SAS name that is not a reserved word in the SAS macro facility.
text
can be any of the following elements:
  • constant text, possibly including SAS data set names, SAS variable names, or SAS statements
  • macro variables, macro functions, or macro program statements
  • any combination of the above
Tip
You might want to include the optional macro-name in the %MEND statement in order to make your program more readable.

Example: Defining a Macro

In the following example, the macro Printit generates a PROC PRINT step.
%macro printit;
proc print data=&syslast (obs=5);
   title "Listing of &syslast data set";
run;
%mend printit;
%printit;
Note: Your output might differ, depending on the value of the &SYSLAST automatic macro variable at the time.
Output 9.1 Printit Macro Output
Printit Macro Output

Compiling a Macro

A Brief Overview

In order to use the Printit macro later in your SAS programs, you must first compile it by submitting the macro definition, as follows:
%macro printit;
proc print data=&syslast (obs=5);
   title "Listing of &syslast data set";
   run;
%mend;
When you submit this code, the word scanner divides the macro into tokens and sends the tokens to the macro processor for compilation. Here is what the macro processor does:
  • checks all macro language statements for syntax errors (non-macro language statements are not checked until the macro is executed).
  • writes error messages to the SAS log if any syntax errors are found in the macro language statements.
  • stores all compiled macro language statements and constant text in a SAS catalog entry if no syntax errors are found in the macro language statements. By default, a catalog named Work.Sasmacr is opened, and a catalog entry named Macro-Name.Macro is created.
That is, if there are no syntax errors in the macro language statements within the macro, the text between the %MACRO statement and the %MEND statement is stored under the name Printit for execution at a later time.

The MCOMPILENOTE= Option

By default, no note is written to the log if a macro program compiles successfully. The MCOMPILENOTE= system option writes a note to the SAS log when a macro has completed compilation.
Syntax, MCOMPILENOTE= system option:
OPTIONS MCOMPILENOTE= NONE | NOAUTOCALL | ALL;
NONE
is the default value, which specifies that no notes are issued to the log.
NOAUTOCALL
specifies that a note is issued to the log for completed macro compilations for all macros except autocall macros.
ALL
specifies that a note is issued to the log for all completed macro compilations.

Example: Using the MCOMPILENOTE Option

A macro might compile but still contain non-macro syntax errors. If there are any macro statement errors, an error message is written to the SAS log, in addition to the note. Here is an example of the note that is written to the log when a macro compiles without errors:
options mcompilenote=all;
%macro printit;
   proc print data=&syslast(obs=5);
      title "Listing of &syslast data set";
   run;
%mend printit;
The following is written to the SAS log.
Log 9.1 SAS Log
NOTE: The macro PRINTIT completed compilation without errors.
      3 instructions 20 bytes

Calling a Macro

A Brief Overview

After the macro is successfully compiled, you can use it in your SAS programs for the duration of your SAS session without resubmitting the macro definition. Just as you must reference macro variables in order to access them in your code, you must call a macro program in order to execute it within your SAS program.
Here are the requirements for using a macro call:
  • It is specified by placing a percent sign (%) before the name of the macro.
  • It can be made anywhere in a program except within the data lines of a DATALINES statement (similar to a macro variable reference).
  • It requires no semicolon because it is not a SAS statement.
To execute the macro Printit you call the macro as follows:
%printit
CAUTION:
A semicolon after a macro call might insert an inappropriate semicolon into the resulting program, leading to errors during compilation or execution.

Example: Calling a Macro

Suppose you have a SAS program that consists of several program steps that create SAS data sets. After each of these program steps, you want to print out the data set that has been created. Remember that the macro Printit prints the most recently created data set. If Printit has been compiled, you can call it after each step in order to print each data set.
proc sort data=sashelp.cars out=cars_mpg;
   by MPG_City;
run;
%printit                                        
proc sort data=sashelp.cars out=cars_msrp;
   by MSRP;
run;
%printit                                        
Output 9.2 %PRINTIT Output 1: Sorted by MPG_City
%PRINTIT Output 1: Sorted By MPG_City
Output 9.3 %PRINTIT Output 2: Sorted by MSRP
%PRINTIT Output 2: Sorted By MSRP

Macro Execution

A Brief Overview

When you call a macro in your SAS program, the word scanner passes the macro call to the macro processor, because the percent sign that precedes the macro name is a macro trigger. Here is what the macro processor does when it receives %macro-name:
  1. It searches the designated SAS catalog (Work.Sasmacr by default) for an entry named macro-name.macro.
  2. It executes compiled macro language statements within macro-name.
  3. It sends any remaining text in macro-name to the input stack for word scanning.
  4. It suspends macro execution when the SAS compiler receives a global SAS statement or when it encounters a SAS step boundary.
  5. It resumes execution of macro language statements after the SAS code executes.
The macro call is processed by the macro processor before any SAS language statements such as DATA steps are compiled or executed. During macro execution, the macro processor can communicate directly with the following elements:
  • both global and local symbol tables. For example, the macro processor can store macro variable values with a %LET statement and can resolve macro variable references.
  • the input stack. For example, the macro processor can generate SAS code for tokenization by the word scanner.

Example: Executing a Macro

This example demonstrates macro execution. Assume that the Printit macro has been compiled and that it has been stored in the Work.Sasmacr catalog.
  1. First, you submit the macro call, as follows:
    %printit
  2. When the word scanner encounters this call, it passes the call to the macro processor. The macro processor searches for the compiled macro in the catalog entry Work.Sasmacr.Printit.Macro.
    %macro printit;
       proc print data=&syslast;
       title "Listing of &syslast data set";
       run;
    %mend printit;
  3. The macro processor begins executing compiled macro language statements. However, in this example, no compiled macro statements are included in the macro.
  4. The macro processor places non-compiled items (SAS language statements) on the input stack, and pauses as the word scanner tokenizes the inserted text. In this example, the macro processor places the PROC PRINT step on the input stack.
    proc print data=&syslast;
       title "Listing of &syslast data set";
    run;
    
  5. The word scanner passes these tokens to the compiler. When the word scanner encounters a macro variable reference such as &SYSLAST, it passes the reference to the macro processor for resolution. The macro processor returns the macro variable value to the input stack, and word scanning continues.
  6. After all of the statements in the PROC PRINT step have been compiled, the PROC PRINT step is executed, and SAS creates output of the most recently created data set.
  7. Once the PROC PRINT step has been executed, the macro processor resumes execution of any remaining macro language statements in the macro (there are none in this example). The macro processor ends execution when it reaches the %MEND statement.
Assume that the most recently created data set is Work.Update_Schedule from an earlier example. Here is the output that is generated by calling the Printit macro.
Figure 9.1 Partial Output: Printit Macro Output: &SYSLAST
Printit Macro Output: &syslast
The following is written to the SAS log when %printit is submitted, assuming that the most recently data set is Work.Update_Schedule.
Log 9.2 SAS Log
NOTE: There were 5 observations read from the data set WORK.UPDATE_SCHEDULE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
Notice that in this SAS log message you see a note from PROC PRINT, but not the PROC PRINT code itself since the call to the macro does not display the text that is sent to the compiler.
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset