Friday, February 26, 2016

Hack 2.4 Creating Your Own Custom ERROR Messages


SAS Programming Professionals,

Did you know that you can create your own custom error messages and have them written to the SAS log? 

The ERROR statement provides a convenient facility for flagging data issues that you uncover while processing SAS data sets.  The format of the ERROR statement is: 

                ERROR <message>;

 …where message is optional; though what’s the point of not using it?

Here is an example:

             data vetted_class;
      set  sashelp.class; 

if sex = "M" and age > 14 then do;
      ERROR "ERROR: MALE SUBJECT ERROR: Subject too old for
             study";
      delete;
end;
      else if sex = "F" and age < 12 then do;
            ERROR "ERROR: FEMALE SUBJECT ERROR: Subject too
                   young for  study";
            delete;
      end;
run;
In this example, observations for males who are older than 14 are flagged as errors and not written to the output SAS data set.  Instead, a custom error message is written to the SAS log.  Observations for females who are younger than 12 are also flagged as errors and not written to the output SAS data set.  A message specifying that particular error is written to the SAS log.

 The log looks, in part, like this: 

ERROR: FEMALE SUBJECT ERROR: Subject too young for study
Name=Joyce Sex=F Age=11 Height=51.3 Weight=50.5 _ERROR_=1 _N_=11
ERROR: MALE SUBJECT ERROR: Subject too old for study
Name=Philip Sex=M Age=16 Height=72 Weight=150 _ERROR_=1 _N_=15
ERROR: MALE SUBJECT ERROR: Subject too old for study
Name=Ronald Sex=M Age=15 Height=67 Weight=133 _ERROR_=1 _N_=17
ERROR: MALE SUBJECT ERROR: Subject too old for study
Name=William Sex=M Age=15 Height=66.5 Weight=112 _ERROR_=1 _N_=19
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.VETTED_CLASS has 15 observations and 5 variables.

When you execute the ERROR statement, SAS sets the _ERROR_ automatic variable to 1, causing it to dump the contents of the Program Data Vector to the SAS log.  That is why you see the values of the “errant” observation in the SAS log.

Note that you do not have to delete “errant” observations as per the example.  (Without the DELETE statement, those “erroneous” observations would be written to the output SAS data set).  Maybe you simply wanted to flag certain conditions and have a custom message written to the log.  You would still use the ERROR statement, but maybe soften the text of the message that follows.  Perhaps something like “NOTE: ….”, or “WARNING:…”, or “DID YOU KNOW THAT:…” would suffice.  Perhaps…
Best of luck in all of your SAS endeavors!

----MMMMIIIIKKKKEEEE

(aka Michael A. Raithel)
Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers


I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

No comments:

Post a Comment