Developing and Debugging Macros

Monitoring Execution with System Options

In the last example, you saw that when you call a macro, the text that is sent to the compiler does not appear in the SAS log. But sometimes you might want to see this text. There are SAS system options that can display information about the execution of a macro in the SAS log. This can be especially helpful for debugging purposes.

The MPRINT Option

When the MPRINT option is specified, the text that is sent to the SAS compiler as a result of macro execution is printed in the SAS log.
General form, MPRINT | NOMPRINT option:
OPTIONS MPRINT | NOMPRINT;
Here is an explanation of the syntax:
NOMPRINT
is the default setting, and specifies that the text that is sent to the compiler when a macro executes is not printed in the SAS log.
MPRINT
specifies that the text that is sent to the compiler when a macro executes is printed in the SAS log.
You might want to specify the MPRINT system option with the following conditions:
  • you have a SAS syntax error or execution error
  • you want to see the generated SAS code.
The MPRINT system option is often synchronized with the SOURCE system option to show, or hide, executed SAS code.

Example

Suppose you want to call the Prtlast macro and to use the MPRINT system option to show the SAS code that results from the macro execution.
Catalog Entry
%macro prtlast;
   proc print data=&syslast (obs=5);
      title "Listing of &syslast data set";
   run;
%mend;
The following sample code creates a data set named Sales, specifies the MPRINT option, and references the Prtlast macro:
data sales;
   price_code=1;
run;
options mprint;
%prtlast
The messages that are written to the SAS log show the text that is sent to the compiler. Notice that the macro variable reference (&SYSLAST) is resolved to the value Work.Sales in the MPRINT messages that are written to the SAS log.
Table 11.3 SAS Log
101  %prtlast
MPRINT(PRTLAST): proc print data=WORK.SALES (obs=5);
MPRINT(PRTLAST): title "Listing of WORK.SALES";
MPRINT(PRTLAST): run;
NOTE: There were 1 observations read from the dataset WORK.SALES.
NOTE: PROCEDURE PRINT used:
      real time           0.04 seconds
      cpu time            0.04 seconds

The MLOGIC Option

Another system option that might be useful when you debug your programs is the MLOGIC option. The MLOGIC option prints messages that indicate macro actions that were taken during macro execution.
General form, MLOGIC | NOMLOGIC option:
OPTIONS MLOGIC | NOMLOGIC;
Here is an explanation of the syntax:
NOMLOGIC
is the default setting, and specifies that messages about macro actions are not printed to the SAS log during macro execution.
MLOGIC
specifies that messages about macro actions are printed to the log during macro execution.
When the MLOGIC system option is in effect, the information that is displayed in SAS log messages includes
  • the beginning of macro execution
  • the values of macro parameters at invocation
  • the execution of each macro program statement
  • whether each %IF condition is true or false
  • the end of macro execution.

Example

Suppose you want to repeat the previous example with only the MLOGIC system option in effect. This sample code creates a data set named Sales, sets the MLOGIC system option, and calls the Prtlast macro.
data sales;
   price_code=1;
run;
options nomprint mlogic;
%prtlast
When this code is submitted, the messages that are written to the SAS log show the beginning and the end of macro processing.
Table 11.4 SAS Log
107   %prtlast
MLOGIC(PRTLAST): Beginning execution.
NOTE: There were 1 observations read from the dataset WORK.SALES.
NOTE: PROCEDURE PRINT used:
      real time           0.02 seconds
      cpu time            0.02 seconds
MLOGIC(PRTLAST): Ending execution.
The MLOGIC option, along with the SYMBOLGEN option, is typically turned
  • on for development and debugging purposes
  • off when the application is in production mode.

Comments in Macro Programs

As with any other programs, your macro programs might benefit from comments. Comments can be especially helpful if you plan to save your macros permanently or to share them with other users. You can place comments within a macro definition by using the macro comment statement.
General form, macro comment statement:
%*comment;
Here is an explanation of the syntax:
comment
can be any message. Like other SAS statements, each macro comment statement ends with a semicolon.

Example

The following code uses macro comments to describe the functionality of the macro:
%macro printit;
   %* The value of &syslast will be substituted appropriately ;
   %* as long as a data set has been created during this session. ;
   proc print data=&syslast(obs=5);
/* Print only the first 5 observations */
   title "Last Created Data Set Is &syslast";
   run;
%mend;
Note: You can also use the comment symbols /* and */ inside a macro. When these symbols appear, the macro processor ignores the text within the comment.
..................Content has been hidden....................

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