Wednesday, September 16, 2009

Colors in the SAS Log

Today I was writing a SAS program, and I wanted a statement I was writing to the log to appear in a different color, so the user could find the information easily by scanning the log. My supervisor wrote a macro program that output a note at the end that appeared red in the log. So I looked up the program to see how he did it.

To my surprise, the macro program uses a simple %put statement with the word ERROR at the beginning, followed immediately by a dash (-). The dash removes the word "ERROR" from the log, replacing it with 5 spaces. This is apparently a default function in SAS. He also added a %str statement inside the word ERROR to break it up, so users do not see the word "ERROR" when the program is included in another program and the user searches the log for errors. Here is the code:

%put ER%str(ROR-) Here is text in red!;

This technique can also be used for the keywords WARNING and NOTE:

%put WA%str(RNING-) Here is text in green!;

%put NO%str(TE-) Here is text in blue!;

Note that the placement of %str() is arbitrary. It could just as well be "%str(W)ARNING-". Keep in mind that the end result is also indented the length of the key word, with blue being the shortest and green being the longest indent. Furthermore, there isn't much point in removing the word "NOTE" since it is generally not associated with error messages.

In the end, only four colors can be used in the log: the default black, red, green, and blue. Blue is the easiest to use, since the key word NOTE does not need to be removed. Having red and green also available, though, is definitely better than nothing!