Error Processing in SAS

Syntax Check Mode

Overview of Syntax Check Mode

If you want processing to stop when a statement in a DATA step has a syntax error, you can enable SAS to enter syntax check mode. You do this by setting the SYNTAXCHECK system option in batch or non-interactive mode, or by setting the DMSSYNCHK system option in the windowing environment.
SAS can enter syntax check mode only if your program creates a data set. If you use the DATA _NULL_ statement, then SAS cannot enter syntax check mode because no data set is created. In this case, using the SYNTAXCHECK or DMSSYNCHK system option has no effect.
In syntax check mode, SAS internally sets the OBS= option to 0 and the REPLACE/NOREPLACE option to NOREPLACE. When these options are in effect, SAS acts as follows:
  • reads the remaining statements in the DATA step or PROC step
  • checks that statements are valid SAS statements
  • executes global statements
  • writes errors to the SAS log
  • creates the descriptor portion of any output data sets that are specified in program statements
  • does not write any observations to new data sets that SAS creates
  • does not execute most of the subsequent DATA steps or procedures in the program (exceptions include PROC DATASETS and PROC CONTENTS)
Note: Any data sets that are created after SAS has entered syntax check mode do not replace existing data sets with the same name.
When syntax checking is enabled, SAS underlines the point where it detects a syntax or semantic error in a DATA step and identifies the error by number. SAS then enters syntax check mode and remains in this mode until the program finishes executing. When SAS enters syntax check mode, all DATA step statements and PROC step statements are validated.

Enabling Syntax Check Mode

You use the SYNTAXCHECK system option to enable syntax check mode when you run SAS in non-interactive or batch mode. You use the DMSSYNCHK system option to enable syntax check mode when you run SAS in the windowing environment. You can use these system options only if your program creates a data set. If you use the DATA _NULL_ statement, then these options are ignored.
To disable syntax check mode, use the NOSYNTAXCHECK and NODMSSYNCHK system options.
In an OPTIONS statement, place the OPTIONS statement that enables SYNTAXCHECK or DMSSYNCHK before the step for which you want it to apply. If you place the OPTIONS statement inside a step, then SYNTAXCHECK or DMSSYNCHK does not take effect until the beginning of the next step.
For more information about these system options, see DMSSYNCHK System Option in SAS System Options: Reference and SYNTAXCHECK System Option in SAS System Options: Reference.

Processing Multiple Errors

Depending on the type and severity of the error, the method that you use to run SAS, and your operating environment, SAS either stops program processing or flags errors and continues processing. SAS continues to check individual statements in procedures after it finds certain types of errors. In some cases SAS can detect multiple errors in a single statement and might issue more error messages for a given situation. This is likely to occur if the statement containing the error creates an output SAS data set.
The following example illustrates a statement with two errors:
data temporary;
   Item1=4;
run;

proc print data=temporary;
   var Item1 Item2 Item3;          
run;
SAS Log: Multiple Program Errors
273  data temporary;
274     Item1=4;
275  run;
NOTE: The data set WORK.TEMPORARY has 1 observations and 1 
      variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      
276  
277  proc print data=temporary;
ERROR: Variable ITEM2 not found.
ERROR: Variable ITEM3 not found.
278     var Item1 Item2 Item3;
279  run;
NOTE: The SAS System stopped processing this step because of 
      errors.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.52 seconds
      cpu time            0.00 seconds
      
280  proc printto; run;
SAS displays two error messages, one for the variable Item2 and one for the variable Item3.
When you are running debugged production programs that are unlikely to encounter errors, you might want to force SAS to abend after a single error occurs. You can use the ERRORABEND system option to do this.

Checkpoint Mode and Restart Mode

Overview of Checkpoint Mode and Restart Mode

When used together, checkpoint mode and restart mode create an environment where batch programs that terminate before completing can be resubmitted without rerunning steps or labeled code sections that have already completed. Execution resumes with either the DATA or PROC step or the labeled code section that was executing when the failure occurred.
A labeled code section is the SAS code that begins with label: outside of a DATA or PROC step and ends with the RUN statement that precedes the next label: that is outside of a DATA or PROC step,. Labels must be unique. Consider using labeled code sections when you want to group DATA or PROC steps that might need to be grouped together because the data for one is dependent on the other.
The following example program has two labeled code sections. The first labeled code section begins with the label readSortData: and ends with the run; statement for proc sort data=mylib.mydata;. The second labeled code section starts with the label report: and ends with the run; statements for proc report data=mylib.mydata;.
readSortData: 
data mylib.mydata;
...more sas code...
run;

proc sort data=mylib.mydata;
...more sas code...
run;

report: 
proc report data=mylib.mydata;
...more sas code...;
run;
endReadSortReport:
Note: The use of label: in checkpoint mode and restart mode is valid only outside of a DATA or PROC statement. Checkpoint mode and restart mode for labeled code sections are not valid for labels within a DATA step or macros.
Checkpoint mode and restart mode can be enabled for either DATA and PROC steps or for labeled code sections, but not both simultaneously. To use checkpoint mode and restart mode on a step-by-step basis, use the step checkpoint mode and the step restart mode. To use checkpoint mode and restart mode based on groups of code sections, use the label checkpoint mode and the label restart mode. Each group of code is identified by a unique label. If you use labels, all steps in a SAS program must belong to a labeled code section.
When checkpoint mode is enabled, SAS records information about DATA and PROC steps or labeled code sections in a checkpoint library. When a batch program terminates prematurely, you can resubmit the program in restart mode to complete execution. In restart mode, global statements are re-executed, macro definitions are recompiled, and macros are re-executed.. SAS reads the data in the checkpoint library to determine which steps or labeled code sections completed. Program execution resumes with the step or the label that was executing when the failure occurred.
The checkpoint-restart data contains information only about the DATA and PROC steps or the labeled code sections that completed and the step or labeled code sections that did not complete. The checkpoint-restart data does not contain the following information:
  • information about macro variables and macro definitions
  • information about SAS data sets
  • information that might have been processed in the step or labeled code section that did not complete
Note: Checkpoint mode is not valid for batch programs that contain the DM statement to submit commands to SAS. If checkpoint mode is enabled and SAS encounters a DM statement, checkpoint mode is disabled and the checkpoint catalog entry is deleted.
As a best practice, if you use labeled code sections, add a label at the end of your program. When the program completes successfully, the label is recorded in the checkpoint-restart data. If the program is submitted again in restart mode, SAS knows that the program has already completed successfully.
If a DATA or PROC step must be re-executed, you can add the global statement CHECKPOINT EXECUTE_ALWAYS immediately before the step. This statement tells SAS to always execute the following step without considering the checkpoint-restart data. It is applicable only to the step that follows the statement. For more information, see CHECKPOINT EXECUTE_ALWAYS Statement in SAS Statements: Reference.
You enable checkpoint mode and restart mode for DATA and PROC steps by using system options when you start the batch program in SAS.
  • STEPCHKPT system option enables checkpoint mode, which indicates to SAS to record checkpoint-restart data
  • STEPCHKPTLIB system option identifies a user-specified checkpoint-restart library
  • STEPRESTART system option enables restart mode, ensuring that execution resumes with the DATA or PROC step indicated by the checkpoint-restart library.
You enable checkpoint mode and the restart mode for labeled code sections by using these system options when you start the batch program in SAS:
  • LABELCHKPT system option enables checkpoint mode for labeled code sections, which indicates to SAS to record checkpoint-restart data.
  • LABELCHKPTLIB system option identifies a user-specified checkpoint-restart library
  • LABELRESTART system option enables restart mode, ensuring that execution resumes with the labeled code section indicated by the checkpoint-restart library.
If you use the Work library as your checkpoint-restart library, you can use the CHKPTCLEAN system option to have the files in the Work library erased after a successful execution of your batch program.
For information, see the following system options in SAS System Options: Reference:

Requirements for Using Checkpoint Mode and Restart Mode

In order for checkpoint mode and restart mode to work successfully, the number and order of the DATA and PROC steps or labeled code sections in the batch program must not change between SAS invocations. By specifying the ERRORABEND and ERRORCHECK system options when SAS starts, SAS terminates for most error conditions in order to maintain valid checkpoint-restart data.
The checkpoint-restart library can be a user-specified library or, if no library is specified, the checkpoint-restart data is saved to the Work library. Always start SAS with the NOWORKTERM and NOWORKINIT system options regardless of whether the checkpoint-restart data is saved to a user-specified library or to the Work library. SAS writes the name of the Work library to the SAS log.
Operating Environment Information: Under UNIX and z/OS operating environments, consider always assigning a checkpoint-restart library when you use the STEPCHKPT option or the LABELCHKPT option. If your site sets the CLEANWORK utility to run at regular intervals, data in the Work library might be lost. Under z/OS, it might not be practical for your site to reuse the Work library in a batch session.
The labels for labeled code sections must be unique. If SAS enters restart mode for a label that is a duplicate label, SAS starts at the first label. The code between the duplicate labels might rerun needlessly.

Setting Up and Executing Checkpoint Mode and Restart Mode

To set up checkpoint mode and restart mode, make the following modifications to your batch program:
  • Add the CHECKPOINT EXECUTE_ALWAYS statement before any DATA and PROC steps that you want to execute each time the batch program is submitted.
  • If your checkpoint-restart library is a user-defined library, you must add the LIBNAME statement that defines the checkpoint-restart libref as the first statement in the batch program. If you use the Work library as your checkpoint library, no LIBNAME statement is necessary.
Once the batch program has been modified, you start the program using the appropriate system options:
  • For checkpoint-restart data that is saved in the Work library, start a batch SAS session that specifies these system options:
    • SYSIN, if required in your operating environment, names the batch program.
    • STEPCHKPT or LABELCHKPT enables checkpoint mode.
    • NOWORKTERM saves the Work library when SAS ends.
    • NOWORKINIT does not initialize the Work library when SAS starts.
    • ERRORCHECK STRICT puts SAS in syntax-check mode when an error occurs in the LIBNAME, FILENAME, %INCLUDE, and LOCK statements.
    • ERRORABEND specifies whether SAS terminates for most errors.
    • CHKPTCLEAN specifies whether to erase files in the Work library and delete the Work library if the batch program runs successfully.
    In the Windows operating environment, the following SAS command starts a batch program in checkpoint mode using the Work library as the checkpoint-restart library:
    sas -sysin 'c:\mysas\myprogram.sas'-stepchkpt -noworkterm -noworkinit
        -errorcheck strict -errorabend -chkptclean
  • For checkpoint-restart data that is saved in a user-specified library, start a batch SAS session that includes these system options:
    • SYSIN, if required in your operating environment, names the batch program.
    • STEPCHKPT or LABELCHKPT enables checkpoint mode.
    • STEPCHKPTLIB or LABELCHKPTLIB specifies the libref of the library where SAS saves the checkpoint-restart data.
    • NOWORKTERM saves the Work library when SAS ends.
    • NOWORKINIT does not initialize the Work library when SAS starts.
    • ERRORCHECK STRICT puts SAS in syntax-check mode when an error occurs in the LIBNAME, FILENAME, %INCLUDE, and LOCK statements.
    • ERRORABEND specifies whether SAS terminates for most errors.
    In the Windows operating environment, the following SAS command starts a batch program in checkpoint mode using a user-specified checkpoint-restart library:
    sas -sysin 'c:\mysas\myprogram.sas' -labelchkpt -labelchkptlib mylibref 
        -noworkterm -noworkinit -errorcheck strict -errorabbend
    In this case, the first statement in myprogram.sas is the LIBNAME statement that defines the mylibref libref.

Restarting Batch Programs

To resubmit a batch SAS session using the checkpoint-restart data that is saved in the Work library, include these system options when SAS starts:
  • SYSIN, if required in your operating environment, names the batch program.
  • STEPCHKPT or LABELCHKPT continues checkpoint mode.
  • STEPRESTART or LABELRESTART enables restart mode, indicating to SAS to use the checkpoint-restart data.
  • NOWORKINIT starts SAS using the Work library from the previous SAS session.
  • NOWORKTERM saves the Work library when SAS ends.
  • ERRORCHECK STRICT puts SAS in syntax-check mode when an error occurs in the LIBNAME, FILENAME, %INCLUDE, and LOCK statements.
  • ERRORABEND specifies whether SAS terminates for most errors.
  • CHKPTCLEAN specifies whether to erase files in the Work library if the batch program runs successfully.
In the Windows operating environment, the following SAS command resubmits a batch program whose checkpoint-restart data was saved to the Work library:
sas -sysin 'c:\mysas\mysasprogram.sas' -stepchkpt -steprestart -noworkinit 
    -noworkterm  -errorcheck strict -errorabend -chkptclean
By specifying the NOWORKTERM system options and either the STEPCHKPT or LABELCHKPT system option, checkpoint mode continues to be enabled once the batch program restarts.
To resubmit a batch SAS session using the checkpoint-restart data that is saved in a user-specified library, include these system options when SAS starts:
  • SYSIN, if required in you operating environment, names the batch program.
  • STEPCHKPT or LABELCHKPT continues checkpoint mode.
  • STEPRESTART or LABELRESTART enables restart mode, indicating to SAS to use the checkpoint-restart data.
  • STEPCHKPTLIB or LABELCHKPTLIB specifies the libref of the checkpoint-restart library.
  • NOWORKTERM saves the Work library when SAS ends.
  • NOWORKINIT does not initialize the Work library when SAS starts.
  • ERRORCHECK STRICT puts SAS in syntax-check mode when an error occurs in the LIBNAME, FILENAME, %INCLUDE, and LOCK statements.
  • ERRORABEND specifies whether SAS terminates for most errors.
In the Windows operating environment, the following SAS command resubmits a batch program whose checkpoint-restart data was saved to a user-specified library:
sas -sysin 'c:\mysas\mysasprogram.sas' -labelchkpt -labelrestart -labelchklib 
    -noworkterm -noworkinit mylibref -errorcheck strict -errorabend

Using System Options to Control Error Handling

You can use the following system options to control error handling (resolve errors) in your program:
BYERR
specifies whether SAS produces errors when the SORT procedure attempts to process a _NULL_ data set.
CHKPTCLEAN
in checkpoint mode or reset mode, specifies whether to erase files in the Work directory if a batch program executes successfully.
DKRICOND=
specifies the level of error detection to report when a variable is missing from an input data set during the processing of a DROP=, KEEP=, and RENAME= data set option.
DKROCOND=
specifies the level of error detection to report when a variable is missing from an output data set during the processing of a DROP=, KEEP=, and RENAME= data set option.
DSNFERR
when a SAS data set cannot be found, specifies whether SAS issues an error message.
ERRORABEND
specifies whether SAS responds to errors by terminating.
ERRORCHECK=
specifies whether SAS enters syntax-check mode when errors are found in the LIBNAME, FILENAME, %INCLUDE, and LOCK statements.
ERRORS=
specifies the maximum number of observations for which SAS issues complete error messages.
FMTERR
when a variable format cannot be found, specifies whether SAS generates an error or continues processing.
INVALIDDATA=
specifies the value that SAS assigns to a variable when invalid numeric data is encountered.
LABELCHKPT
specifies whether SAS checkpoint-restart data is to be recorded for a batch program that contains labeled code sections.
LABELCHKPTLIB
specifies the libref of the library where checkpoint-restart data is saved for labeled code sections.
LABELRESTART
specifies whether to execute a batch program by using checkpoint-restart data for labeled code sections.
MERROR
specifies whether SAS issues a warning message when a macro-like name does not match a macro keyword.
QUOTELENMAX
if a quoted string exceeds the maximum length allowed, specifies whether SAS writes a warning message to the SAS log.
SERROR
specifies whether SAS issues a warning message when a macro variable reference does not match a macro variable.
STEPCHKPT
specifies whether checkpoint-restart data is to be recorded for a batch program.
STEPCHKPTLIB
specifies the libref of the library where checkpoint-restart data is saved.
STEPRESTART
specifies whether to execute a batch program by using checkpoint-restart data.
VNFERR
specifies whether SAS issues an error or warning when a BY variable exists in one data set but not another data set. SAS only issues these errors or warnings when processing the SET, MERGE, UPDATE, or MODIFY statements.
For more information about SAS system options, see SAS System Options: Reference.

Using Return Codes

In some operating environments, SAS passes a return code to the system, but the way in which return codes are accessed is specific to your operating environment.
Operating Environment Information: For more information about return codes, see the SAS documentation for your operating environment.

Other Error-Checking Options

To help determine your programming errors, you can use the following methods:
  • the _IORC_ automatic variable that SAS creates (and the associated IORCMSG function) when you use the MODIFY statement or the KEY= data set option in the SET statement
  • the ERRORS= system option to limit the number of identical errors that SAS writes to the log
  • the SYSRC and SYSMSG functions to return information when a data set or external-files access function encounters an error condition
  • the SYSRC automatic macro variable to receive return codes
  • the SYSERR automatic macro variable to detect major system errors, such as out of memory or failure of the component system
  • log control options:
    MSGLEVEL=
    controls the level of detail in messages that are written to the SAS log.
    PRINTMSGLIST
    controls the printing of extended lists of messages to the SAS log.
    SOURCE
    controls whether SAS writes source statements to the SAS log.
    SOURCE2
    controls whether SAS writes source statements included by %INCLUDE to the SAS log.