The Macro facility is a powerful and flexible tool, but sloppy techniques can make macros one of your biggest headaches. Poorly handled macros may run without error but leave your users with unexpected results and litter your system with unwanted files and variables.
Two presenters at the SouthEastern SAS Users Group (SESUG) conference presented ways to improve your macros. While neither author promises an easy fix, they do promise your users will appreciate the results.
Frank DiIorio suggests a 12-step program for building a better macro. Because they are often used in longer, more complex SAS programs, he believes that SAS programmers must exercise self-discipline to achieve workmanlike results, keep systems manageable, and simplify coding and debugging. Here's an overview of DiIorio's tips (as presented by Paul Dorfman):
- Know when a macro is and is not necessary.
- Conceptually separate utilities and applications.
- Clearly document the macro.
- Use keyword-style parameters.
- Use consistent program structure.
- Build a means for user and programmers to communicate with macro.
- Control macro variable scope.
- Implement diagnostic and debugging code.
- Use built-in macro tools.
- Build other tools you need.
- Adopt a software development mindset.
- Know when it's time for a rewrite.
Look for details behind each of these steps in DiIorio's paper "Building a Better Macro" in SESUG proceedings when they are published.
David Abbott and Rebecca McNeill agree that unexpected side effects are pervasive anywhere macros are used. They diagnose most of these problems as the result of macros overwriting at least eight other types of SAS names, such as other macro variables, datasets, or dataset variable names. One easy fix is to develop a set of consistent naming conventions, but this solution may not be sufficient:
- If conventions aren't used 100 percent of the time.
- If you don't document them well enough.
- If you are using deeply nested macros.
Their paper "Make Macros Safe for Others to Use" in SESUG proceedings offers three programming techniques that macro writers can use to avoid nasty surprises for users, making their programs easier to debug, and ensure more predictable results.