Macro Facility Error Messages and Debugging |
Developing Macros in a Layered Approach |
Because the macro facility is such a powerful tool, it is also complex, and debugging large macro applications can be extremely time-consuming and frustrating. Therefore, it makes sense to develop your macro application in a way that minimizes the errors and that makes the errors that do occur as easy as possible to find and fix. The first step is to understand what type of errors can occur and when they manifest themselves. Then, develop your macros using a modular, layered approach. Finally, use some built-in tools such as system options, automatic macro variables, and the %PUT statement to diagnose errors.
Note: To receive certain important warning messages about unresolved macro names and macro variables, be sure the SERROR System Option and MERROR System Option are in effect. See System Options for Macros for more information about these system options.
Encountering Errors |
When the word scanner processes a program and finds a token in the form of & or % , it triggers the macro processor to examine the name token that follows the & or % . Depending on the token, the macro processor initiates one of the following activities:
macro variable resolution
macro open code processing
macro compilation
macro execution
An error can occur during any one of these stages. For example, if you misspell a macro function name or omit a necessary semicolon, that is a syntax error during compilation. Syntax errors occur when program statements do not conform to the rules of the macro language. Or, you might refer to a variable out of scope, causing a macro variable resolution error. Execution errors (also called semantic errors) are usually errors in program logic. They can occur, for example, when the text generated by the macro has faulty logic (statements not executed in the right order or in the way you expect).
Of course, even if your macro code is perfect, that does not guarantee that you will not encounter errors caused by plain SAS code. For example, you might encounter a libref that is not defined, a syntax error in open code (that is, outside of a macro definition), or a typo in the code your macro generates. Typically, error messages with numbers are plain SAS code error messages. Error messages generated by the macro processor do not have numbers.
Developing Bug-free Macros |
When programming in any language, it is good technique to develop your code in modules. That is, instead of writing one massive program, develop it piece by piece, test each piece separately, and put the pieces together. This technique is especially useful when developing macro applications because of the two-part nature of SAS macros: macro code and the SAS code generated by the macro code.
Another good idea is to proofread your macro code for common mistakes before you submit it.
The following list outlines some key items to check for:
the names in the %MACRO and %MEND statements match, and there is a %MEND for each %MACRO.
the number of %DO statements matches the number of %END statements.
%TO values for iterative %DO statements exist and are appropriate.
all statements end with semicolons.
comments begin and end correctly and do not contain unmatched single quotation marks.
macro variable references begin with & and macro statements begin with % .
macro variables created by CALL SYMPUT are not referenced in the same DATA step in which they are created.
statements that execute immediately (such as %LET) are not part of conditional DATA step logic.
single quotation marks are not used around macro variable references (such as in TITLE or FILENAME statements). When used in quoted strings, macro variable references resolve only in strings marked with double quotation marks.
macro variable values do not contain any keywords or characters that could be interpreted as mathematical operators. (If they do contain such characters, use the appropriate macro quoting function.)
macro variables, %GOTO labels, and macro names do not conflict with reserved SAS and host environment keywords.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.