Basic Concepts

Defining a Macro

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.
General form, %MACRO statement, and %MEND statement:
%MACRO macro-name;
text
%MEND <macro-name>;
Here is an explanation of the syntax:
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
  • 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 macro-name in the %MEND statement in order to make your program more readable. However, the inclusion of macro-name in the %MEND statement is entirely optional.

Example

This program creates a macro named Prtlast that prints the most recently created data set. (Remember that the automatic macro variable SYSLAST stores the name of the most recently created data set.)
%macro prtlast;
   proc print data=&syslast (obs=5);
   title "Listing of &syslast data set";
   run;
%mend;

Compiling a Macro

In order to use this macro later in your SAS programs, you must first compile it by submitting the macro definition, as follows:
%macro prtlast;
   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. The macro processor
  • 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 and creates a dummy (non-executable) macro 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 Prtlast for execution at a later time.
Note: You can also store a compiled macro in a permanent SAS catalog. You can learn how to do this in Storing Macro Programs.

The MCOMPILENOTE= Option

The MCOMPILENOTE= option causes a note to be issued to the SAS log when a macro has completed compilation.
General form, MCOMPILENOTE= option:
OPTIONS MCOMPILENOTE= NONE | NOAUTOCALL | ALL;
The option can take one of the three values listed. Here is an explanation of the syntax:
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.
Note: You can learn more about autocall macros in Storing Macro Programs.

Example

A macro might actually compile and still contain errors. If there are any 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 mymacro;
%mend mymacro;
Table 11.1 SAS Log
1    options mcompilenote=all;
2    %macro mymacro;
3    %mend mymacro;
NOTE: The macro MYMACRO completed compilation without errors.

Calling a Macro

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.
A macro call
  • is specified by placing a percent sign (%) before the name of the macro
  • can be made anywhere in a program except within the data lines of a DATALINES statement (similar to a macro variable reference)
  • requires no semicolon because it is not a SAS statement.
To execute the macro Prtlast you would call the macro as follows:
%prtlast
CAUTION:
A semicolon after a macro call might insert an inappropriate semicolon into the resulting program, leading to errors during compilation or execution.
Macros come in three types, depending on how they are called: name style, command style, and statement style. Of the three, name style is the most efficient. This is because calls to name style macros always begin with a percent sign (%), which immediately tells the word scanner to pass the token to the macro processor. With the other two types, the word scanner does not know immediately whether the token should be sent to the macro processor or not. Therefore, time is wasted while the word scanner determines this. All of the macros in this chapter are name style macros.

Example

Suppose a SAS program consists of several program steps that create SAS data sets. Suppose that after each of these program steps that you want to print out the data set that has been created. Remember that the macro Prtlast prints the most recently created data set. If Prtlast has been compiled, you can call it after each step in order to print each data set.
proc sort data=sasuser.courses out=courses;
   by course_code;
run;

%prtlast

proc sort data=sasuser.schedule out=schedule;
   by begin_date;
run;

%prtlast

proc sort data=sasuser.students out=students;
   by student_name;
run;

%prtlast
Note: The example above is simply meant to show you how you can incorporate a macro into your SAS program. Although this is a valid use of the Prtlast macro, this might not be the best way to code this example. Since the Prtlast macro uses no conditional logic or macro programming statements and it makes no decisions, this example does not illustrate the full power of a macro program. In the rest of this chapter, you see examples of macro programs that are more useful than this one.

Macro Execution

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. When the macro processor receives %macro-name, it
  1. searches the designated SAS catalog (Work.Sasmacr by default) for an entry named Macro-name.Macro
  2. executes compiled macro language statements within Macro-name
  3. sends any remaining text in Macro-name to the input stack for word scanning
  4. suspends macro execution when the SAS compiler receives a global SAS statement or when it encounters a SAS step boundary
  5. resumes execution of macro language statements after the SAS code executes.
Later in this chapter you see detailed examples of macro execution. These examples make more sense once you have learned how to write a more complex macro program than you have seen so far in this chapter.
For now, remember that 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:
  • 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.
Note: You learn more about global and local symbol tables later in this chapter.

Example

This example demonstrates macro execution. Assume that the Prtlast macro has been compiled and that it has been stored in the Work.Sasmacr catalog.
  1. First, you submit the macro call, as follows:
    %prtlast
  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.Prtlast.Macro.
    Catalog Entry
    %macro prtlast;
       proc print data=&syslast(obs=5);
       title "Listing of &syslast data set";
       run;
    %mend;
  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 noncompiled 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.
    Input Stack
    proc print data=&syslast(obs=5);
       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 that includes only the first five observations 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.Practice (which is a copy of Sasuser.Courses). Here is the output that is generated by calling the Prtlast macro.
Prtlast macro
Here is an example of messages that are written to the SAS log when %prtlast is submitted, assuming that the most recently created data set is Work.Practice.
Table 11.2 SAS Log
37   %prtlast

NOTE: Writing HTML Body file: sashtm3.htm
NOTE: There were 5 observations read from the data set
      WORK.PRACTICE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 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.
This example depicts the processing and execution of a simple macro. Later in this chapter you see how more-complex macros that include compiled macro language statements are handled by the macro processor.
..................Content has been hidden....................

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