Search code examples
sasraiserror

Raise error in SAS EG


I have a SAS EG program that checks if a file exists and refuses to replace it if it dose exists.

When this happens I want SAS EG to show an error on that program and display a meaningful custom error message in the log. And I want the error message to look the same as other error messages people are used to seeing in the log so they pay attention to it.

For now I have just imported a file with a name of my error message. This is not great because it appends file not found messages to the message I want to display.

Is there any way I can declare that there is an error and provide an error message to put in the LOG


Solution

  • The SAS log uses a very simple way to change text color: the first word in the line of text to display in the log. You can recreate ERROR, WARNING, and NOTE messages in both %put and put statements.

    %put NOTE: This is a note;
    %put WARNING: This is a warning;
    %put ERROR: I AM ERROR;
    

    For example, let's say you want to display an error if a certain value is missing.

    data have;
        input var;
        datalines;
    1
    .
    2
    3
    .
    ;
    run;
    
    %put NOTE: Now checking for missing values...;
    
    data _null_;
        set have;
    
        if(missing(var) ) then 
           put 'ERROR: A missing value for "var" was found at observation ' _N_;
    run;
    

    You can also do this with macros, such as creating a flag during certain check steps, or using the &syserr automatic macro variable.

    %macro check;
        %let e  = 0;
    
        data _null_;
            set have;
    
            if(missing(var) ) then call symput('e', 1);
        run;
    
        %if(&e) %then %put ERROR: An error was found.;
    %mend;