7
votes

I need to get SAS to trigger an error if a certain condition is not meet i have tried to use abort return n, abort abend etc.. but they all seems to to disconnect the entire session where i only want to get an error like with syntax etc.

It is a sas propgram only to be run from SAS-EG in interactive mode with prompts

my code:

DATA _NULL_;
IF prxmatch("/^TBDLZL\d{4}_[A-Z]/",&tablename_in) eq 0 then do;
    put "error table name &tablename_in does not match";
    ABORT RETURN 15; 
END;

RUN;

any suggestions ?

2

2 Answers

5
votes

Use the ABORT CANCEL statement. The data step will stop running and the following steps in the submitted code will not be processed.

For example:

data _null_;
  set sashelp.class;
  if name = "John" then do;
    put 'ERR' 'OR: My error message';
    abort cancel;
  end;
run;

* This step is not done due to earlier ABORT CANCEL;
data _null_;
  set sashelp.class;
  where name like 'J%';
run;

From Help:

CANCEL
causes the execution of the submitted statements to be canceled. Actions depend on the method of operation.

  • batch mode and noninteractive mode

    • terminates the entire SAS program and SAS system.
    • writes an error message to the SAS log.
  • windowing environment and interactive line mode

    • clears only the current submitted program.
    • does not affect other subsequent submitted programs.
    • writes an error to the SAS log.
  • workspace server and stored process server

    • clears only the currently submitted program.
    • does not affect other subsequent submit calls.
    • writes an error message to the SAS log.
  • SAS IntrNet application server

    • creates a separate execution for each request and submits the request code. A CANCEL argument in the request code clears the current submitted code but does not terminate the execution or the SAS session.
5
votes

You can try to use:

put 'ERROR:' '/*customize error text here*/';

If you want to stop executing data step you can use stop statement, for example:

DATA _NULL_;
   IF prxmatch("/^TBDLZL\d{4}_[A-Z]/",&tablename_in) eq 0 then do;
       put 'ERROR:' "table name &tablename_in" does not match;
       stop; 
   END;
RUN;

If you have macros and you want to get ERROR message at macro execute step you can use %put:

%put ERROR: /*customize error text here*/;

Thanks @Tom

Note the "trick" of breaking the word ERROR(put ‘ERR’ ‘OR:’...)into two parts is only needed if you are using a dumb search of your logs for errors. Normal SAS error messages in the log always appear at the beginning of the line. SAS/Studio for example does not falsely mark the program lines that contain ERROR as if they were actual errors.

Thanks @MichaelKersten

Another neat trick for multi-line NOTEs, WARNINGS and ERRORS is to replace the ":" with a "-" for the second and consecutive lines. Example:

%put WARNING: first line of warning; 
%put WARNING- second line of warning;