5 ways to exit from a SAS data step or a Program

Several statements, options, functions, and programming alternatives 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. Several exit strategies depend on the objective.

1. STOP Statement

The STOP statement stops the 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.

2. ABORT Statement

ABORT statement is used to stop executing the current DATA step, SAS job, or even a SAS session.

Syntax:

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.

Arguments

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.

Example:

%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.

3. ENDSAS

ENDSAS Terminates a SAS job or session after the current DATA, or PROC step executes.

Note: ENDSAS statements are always executed at the point that they are encountered in a DATA step. Use the ABORT RETURN statement to stop processing when an error condition occurs (for example, in the clause of an IF-THEN statement or a SELECT statement).

The entire job is terminated in the following data step if the variable X is ever less than or equal to zero.
data new;
	set old;
	lnx=log(x);
	if _error_ then
		endsas;
run;

4. RUN CANCEL

The CANCEL option on the RUN statement prevents the current step from being executed. This is a quick way to prevent code from executing instead of 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.

5. ERRORABEND

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.

Syntax

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 handles errors normally, issues an error message, sets OBS=0, and goes into a syntax-check mode (in batch mode only).

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.