Did you catch that SAS error?
The use of automated scripts have made it extremely important to understand error conditions in a programmatic way. Here are a couple of the ways you can check for errors in your automated SAS scripts.
SYSERR - This macro variable should have a value of 0 (zero) if the data or proc step completed successfully.
data new_ds;
set old_ds;
put something here;
run;
%put NOTE: This step returned a value of $SYSERR;
SYSCC - This is the value that SAS would return to the operating system if it were to exit at that point. You can use this to see if there were errors in your script up until this point. The value of SYSERR is reset upon each data or proc step. However, SYSCC doesn't reset unless the user resets it within the script. This can be useful if you want to force SAS to exit in a success or fail status.
NOTE - It is important to know that it is well documented that both of these variables have flaws. There are conditions that don't trigger errors that might not be intuitive. If you are relying heavily on these variables for code success, you should take a minute to read the SAS documentation around what does and doesn't constitute an error.
SAS Conditional programming – If Then
One of the great things about SAS is the use of Macros and conditional programming. The ability to split the code using If - Then processing allows one piece of code to accomplish many things. Here is the basic syntax for If - Then statements in a SAS macro.
%let error1=;
%if %length(&error1) ~=0 %then
%put WARNING: There is an error!;
%else
%put NOTE: There is no error.;
This is the basic example where you want to process one command given a condition. Lets say that you want to process a whole section of code depending on a single condition. See below:
%let error1=;
%if %length(&error1) ~=0 %then %do;
%put WARNING: There is an error!;
<and any other code you want to run here>
%end;
%else
%put NOTE: There is no error.;
<and any other code you want to run here>
%end;
Note that the %do command allows you to run a whole block of code. Be sure to include the %end statement at the end of the code block. Also, the %else portion of both examples are completely optional.