Writing messages to SAS Log in Color

Trying to debug a macro with options mtrace mprint mlogic symbolgen; activated? Good luck in being able to read the log file muchless finding the error without using GREP.

Intro the necessity of being able to send messages to the log in different colors. But how? Hidden deep within SAS’ documentation in the details section of %put, reveals options to write custom error, warning, and note messages. These messages will be able to easily be differentiated from traditional black log text output.

The option to enable color falls from:

  • %PUT <ERROR: | WARNING: | NOTE: > <Some Text for Log>
    • Prefixes ERROR, WARNING, or NOTE to message
  • %PUT <ERROR- | WARNING- | NOTE- > <Some Text for Log>
    • Removes prefixed ERROR, WARNING, or NOTE and replaces the word with a five-space indent.
    • Meant to serve as a way to facilitate multi-line statements.
%put ERROR: Traditional Red Error Text Prefixed with word Error;
%put WARNING: Traditional Green Warning Text Prefixed with word Warning;
%put NOTE: Traditional Blue Note Text Prefixed with word Note;
%put Traditional Black Standard Log Text;

There are several ways to implement color using these options. Below, I choose to present a class wrapper approach to writing error messages. The benefits of this approach are two-fold: 1. Consistent error messages across the program, 2. Ability to modify how errors are handled.

/**
 * Writes messages to log file.
 * @param arg Value to be written to logfile.
 */
%macro log_message(arg);
           %put &arg.;
%mend;

/**
 * Writes a blue-colored message to log file prefixed by note
 * @param e Note text.
 */
%macro throw_note(e);
           %log_message(NOTE: &e.);
%mend;

/**
 * Writes a green-colored message to log file prefixed by warning.
 * @param e Warning text.
 */
%macro throw_warning(e);
           %log_message(WARNING: &e.);
%mend;

/**
 * Writes a red-colored message to log file prefixed by error.
 * Terminates the program.
 * @param e Error text.
 */
%macro throw_error(e);
           %log_message(ERROR: &e.);
           %abort;
%mend;
comments powered by Disqus