Macro headaches? Learn how to prevent them


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): 

  1. Know when a macro is and is not necessary.
  2. Conceptually separate utilities and applications.
  3. Clearly document the macro.
  4. Use keyword-style parameters.
  5. Use consistent program structure.
  6. Build a means for user and programmers to communicate with macro.
  7. Control macro variable scope.
  8. Implement diagnostic and debugging code.
  9. Use built-in macro tools.
  10. Build other tools you need.
  11. Adopt a software development mindset.
  12. 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.



About Author

Christina Harvey

Principal Marketing Specialist

Christina Harvey is an editor for SAS External Communications. She has more than 20 years experience as a technical writer and communications specialist for SAS.

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top