There are a number of statements, options, functions, and programming alternatives that are well suited to controlling how and when all or a portion of a program can be stopped conditionally. Sometimes we want to control the flow of the individual data steps of the program. This may include the termination of a step or even the SAS job itself. There are several exit strategies depending on the objective.
The STOP statement stops execution of the current DATA step immediately and resumes processing the statements after the end of the current DATA step.
SAS writes a data set for the current DATA step. However, the observation being processed when STOP executes is not added. The STOP statement can be used alone or in an IF-THEN statement or SELECT group.
STOP does not set the system error conditions and the job continues executing with the next step.
ABORT statement is used to stop executing the current DATA step, SAS job, or even a SAS session.
ABORT <ABEND | CANCEL <FILE> | RETURN> <n> <NOLIST>;
ERROR: Execution terminated by an ABORT statement at line 73 column 18.
The ABORT statement is usually used when error conditions are identified. Like the STOP statement, ABORT also stops the execution of the current data step. The behaviour of this statement depends to some extent on the operating system and whether the job is being run under batch or interactive modes.
ABORT writes an error message to the log and sets the system error conditions. In batch mode, ABORT sets OBS=0 and continues limited processing.
In interactive mode, ABORT acts like STOP. The ABORT statement can be used with two options, ABEND and RETURN.
ABEND – With the ABEND option, ABORT writes an error message to the log and returns control to the host system as an abnormal termination.
CANCEL <FILE> – It causes the execution of the submitted statements to be cancelled. Actions depend on the method of operation.
FILE – when coded as an option to the CANCEL argument in an autoexec file or in a %INCLUDE file, causes only the contents of the autoexec file or %INCLUDE file to be cleared by the ABORT statement. Other submitted source statements are executed after the autoexec file or %INCLUDE file.
RETURN The RETURN option is the same as ABEND but exits from SAS as a normal termination. For operating systems that support it, a return code can also be specified with the ABEND and RETURN options.
n is an integer value that enables you to specify a condition code.
- when used with the CANCEL argument, the value is placed in the SYSINFO automatic macro variable.
- when not used with the CANCEL argument, the error code that is returned by SAS is ERROR. The value of ERROR depends on the operating system. The condition code n is returned to the operating system as the final SAS System exit code.
NOLIST – It suppresses the output of all variables to the SAS log.
%macro sort(inputdsn=, ByVar=, outDSN=); %if %length(&inputdsn)=0 %then %do; %put ERROR: Input Dataset must be specified; %abort cancel; %end; %if %length(&Byvar)=0 %then %do; %put ERROR: By Variable must be specified; %abort cancel; %end; proc sort data=&inputdsn. out=&outdsn.; by &byvar.; run; %if &syserr. ne 0 %then %do; %abort cancel; %end; %mend; %sort(inputdsn=sashelp.class , byvar=, outdsn=class);
ERROR: By Variable must be specified ERROR: Execution canceled by an %ABORT CANCEL statement.
Terminates a SAS job or session after the current DATA or PROC step executes.
ENDSAS is most useful in interactive or windowing sessions.
data new; set old; lnx=log(x); if _error_ then endsas; run;
The CANCEL option on the RUN statement prevents the current step from being executed. This is a quick way to prevent code from executing as opposed to using comments.
You can skip a code with the RUN CANCEL statement. Including the option CANCEL on the run statement terminates the current step without executing it.
proc print data=sashelp.class; var age; run cancel;
SAS also prints a message that indicates that the step was not executed.
WARNING: The procedure was not executed at the user's request.
The Cancel option has no effect where the data step has the datalines statement or the run statement has no effect, such as with PROC SQL. Also, it has no effect when you use the KILL option with PROC DATASETS.
Read more on Skipping codes.
Like the ABORT and ENDSAS statements, the system option ERRORABEND specifies how SAS responds to errors. terminates the SAS session or job. Since this is an option, no IF-THEN-ELSE processing is required.
As soon as the first error is encountered the job terminates. This option does not terminate for data errors, but is primarily directed to syntax errors and other errors that would otherwise cause the system to go into syntax check mode.
When ERRORABEND is used in an interactive session, termination may also eliminate the LOG. Thus any clues as to the reason for the termination will also be eliminated.
ERRORABEND | NOERRORABEND
ERRORABEND – It specifies that SAS stops abnormally for most errors (including syntax errors) that would normally cause it to issue an error message, set OBS=0, and go into a syntax-check mode (in batch mode, only). ERRORABEND does not affect how SAS handles notes, such as invalid data messages.
NOERRORABEND – It specifies that SAS handle errors normally, that is, issue an error message, set OBS=0, and go into a syntax-check mode (in batch mode, only).