O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

In the %IF statement, the values of WORD being tested are ambiguous — they could
also be interpreted as the numeric operators AND and OR. Therefore, SAS generates the
following error messages in the log:
ERROR: A character operand was found in the %EVAL function or %IF
condition where a numeric operand is required. The condition
was:word = and or &word = but or &word = or
ERROR: The macro will stop executing.
To fix this problem, use the quoting functions %BQUOTE and %STR, as in the
following corrected program:
%macro conjunct(word= );
%if %bquote(&word) = %str(and) or %bquote(&word) = but or
%bquote(&word) = %str(or) %then
%do %put *** &word is a conjunction. ***;
%else
%do %put *** &word is not a conjunction. ***;
%mend conjunct;
In the corrected program, the %BQUOTE function quotes the result of the macro
variable resolution (in case the user passes in a word containing an unmatched quotation
mark or some other odd value). The %STR function quotes the comparison values AND
and OR at compile time, so they are not ambiguous. You do not need to use %STR on
the value BUT, because it is not ambiguous (not part of the SAS or macro languages).
For more information, see Chapter 7, “Macro Quoting,” on page 81.
Debugging Techniques
Using System Options to Track Problems
The SAS system options MLOGIC, MLOGICNEST, MPRINT, MPRINTNEST, and
SYMBOLGEN can help you track the macro code and SAS code generated by your
macro. Messages generated by these options appear in the SAS log, prefixed by the
name of the option responsible for the message.
Note: Whenever you use the macro facility, use the following macro options: MACRO,
MERROR, and SERROR. SOURCE is a system option that is helpful when using
the macro facility. It is also helpful to use the SOURCE2 system option when using
the %INCLUDE.
Although the following sections discuss each system option separately, you can, of
course, combine them. However, each option can produce a significant amount of
output, and too much information can be as confusing as too little. So, use only those
options that you think you might need and turn them off when you complete the
debugging.
Tracing the Flow of Execution with MLOGIC
The MLOGIC system option traces the flow of execution of your macro, including the
resolution of parameters, the scope of variables (global or local), the conditions of macro
expressions being evaluated, the number of loop iterations, and the beginning and end of
each macro execution. Use the MLOGIC option when you think a bug lies in the
program logic (as opposed to simple syntax errors).
136 Chapter 10 Macro Facility Error Messages and Debugging
Note: MLOGIC can produce a lot of output, so use it only when necessary, and turn it
off when debugging is finished.
In the following example, the macro FIRST calls the macro SECOND to evaluate an
expression:
%macro second(param);
%let a = %eval(&param); &a
%mend second;
%macro first(exp);
%if (%second(&exp) ge 0) %then
%put **** result >= 0 ****;
%else
%put **** result < 0 ****;
%mend first;
options mlogic;
%first(1+2)
Submitting this example with option MLOGIC shows when each macro starts execution,
the values of passed parameters, and the result of the expression evaluation.
MLOGIC(FIRST): Beginning execution.
MLOGIC(FIRST): Parameter EXP has value 1+2
MLOGIC(SECOND): Beginning execution.
MLOGIC(SECOND): Parameter PARAM has value 1+2
MLOGIC(SECOND): %LET (variable name is A)
MLOGIC(SECOND): Ending execution.
MLOGIC(FIRST): %IF condition (%second(&exp) ge 0) is TRUE
MLOGIC(FIRST): %PUT **** result >= 0 ****
MLOGIC(FIRST): Ending execution.
Nesting Information Generated by MLOGICNEST
MLOGICNEST allows the macro nesting information to be written to the SAS log in the
MLOGIC output. The setting of MLOGICNEST does not imply the setting of MLOGIC.
You must set both MLOGIC and MLOGICNEST in order for output (with nesting
information) to be written to the SAS log.
For more information and an example, see “MLOGICNEST System Option” on page
373.
Examining the Generated SAS Statements with MPRINT
The MPRINT system option writes to the SAS log each SAS statement generated by a
macro. Use the MPRINT option when you suspect your bug lies in code that is generated
in a manner that you did not expect.
For example, the following program generates a simple DATA step:
%macro second(param);
%let a = %eval(&param); &a
%mend second;
%macro first(exp);
data _null_;
var=%second(&exp);
Debugging Techniques 137

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required