SAS 9.1.3 Language Reference: Concepts, Third Edition, Volumes 1 and 2
Types of Errors in SAS
Summary of Types of Errors That SAS Recognizes
SAS performs error processing during both the compilation and the execution phases of SAS processing. You can debug SAS programs by understanding processing messages in the SAS log and then fixing your code. You can use the DATA Step Debugger to detect logic errors in a DATA step during execution.
SAS recognizes five types of errors.
Type of error | When this error occurs | When the error is detected |
---|---|---|
syntax | when programming statements do not conform to the rules of the SAS language | compile time |
semantic | when the language element is correct, but the element may not be valid for a particular usage | compile time |
execution-time | when SAS attempts to execute a program and execution fails | execution time |
data | when data values are invalid | execution time |
macro- related | when you use the macro facility incorrectly | macro compile time or execution time, DATA or PROC step compile time or execution time |
Syntax Errors
Syntax errors occur when program statements do not conform to the rules of the SAS language. Here are some examples of syntax errors:
-
misspelled SAS keyword
-
unmatched quotation marks
-
missing a semicolon
-
invalid statement option
-
invalid data set option.
When SAS encounters a syntax error, it first attempts to correct the error by attempting to interpret what you mean, then continues processing your program based on its assumptions. If SAS cannot correct the error, it prints an error message to the log.
In the following example, the DATA statement is misspelled, and SAS prints a warning message to the log. Because SAS could interpret the misspelled word, the program runs and produces output.
date temp; x=1; run; proc print data=temp; run;
Output 9.1: SAS Log: Syntax Error (misspelled key word)
|
1 date temp; ---- 14 WARNING 14-169: Assuming the symbol DATA was misspelled as date. 2 x=1; 3 run; NOTE: The data set WORK.TEMP has 1 observations and 1 variables. NOTE: DATA statement used: real time 0.17 seconds cpu time 0.04 seconds 4 5 proc print data=temp; 6 run; NOTE: PROCEDURE PRINT used: real time 0.14 seconds cpu time 0.03 seconds
|
Some errors are explained fully by the message that SAS prints in the log; other error messages are not as easy to interpret because SAS is not always able to detect exactly where the error occurred. For example, when you fail to end a SAS statement with a semicolon, SAS does not always detect the error at the point where it occurs because SAS statements are free-format (they can begin and end anywhere ). In the following example, the semicolon at the end of the DATA statement is missing. SAS prints the word ERROR in the log, identifies the possible location of the error, prints an explanation of the error, and stops processing the DATA step.
data temp x=1; run; proc print data=temp; run;
Output 9.2: SAS Log: Syntax Error (missing semicolon)
|
1 data temp 2 x=1; - 76 ERROR 76-322: Syntax error, statement will be ignored. 3 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: DATA statement used: real time 0.11 seconds cpu time 0.02 seconds 4 5 proc print data=temp; ERROR: File WORK.TEMP.DATA does not exist. 6 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE PRINT used: real time 0.06 seconds cpu time 0.01 seconds
|
Whether subsequent steps are executed depends on which method of running SAS you use, as well as on your operating environment.
Semantic Errors
Semantic errors occur when the form of the elements in a SAS statement is correct, but the elements are not valid for that usage. Semantic errors are detected at compile time and can cause SAS to enter syntax check mode. (For a description of syntax check mode, see "Syntax Check Mode" on page 156.)
Examples of semantic errors include the following:
-
specifying the wrong number of arguments for a function
-
using a numeric variable name where only a character variable is valid
-
using illegal references to an array.
In the following example, SAS detects an illegal reference to the array ALL.
data _null_; array all{*} x1-x5; all=3; datalines; 1 1.5 . 3 2 4.5 3 2 7 3 . . ; run;
Output 9.3: SAS Log: Semantic Error (illegal reference to an array)
|
cpu time 0.02 seconds 1 data _null_; 2 array all{*} x1-x5; ERROR: Illegal reference to the array all. 3 all=3; 4 datalines; NOTE: The SAS System stopped processing this step because of errors. NOTE: DATA statement used: real time 2.28 seconds cpu time 0.06 seconds 10 ; 11
|
The following is another example of a semantic error. In this DATA step, the libref SOMELIB has not been previously assigned in a LIBNAME statement.
data test; set somelib.old; run;
Output 9.4: SAS Log: Semantic Error (libref not previously assigned)
|
cpu time 0.00 seconds 1 data test; ERROR: Libname SOMELIB is not assigned. 2 set somelib.old; 3 run; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0 observations and 0 variables. NOTE: DATA statement used: real time 0.17 seconds
|
Execution-Time Errors
Definition
Execution-time errors are errors that occur when SAS executes a program that processes data values. Most execution-time errors produce warning messages or notes in the SAS log but allow the program to continue executing. [*] The location of an execution-time error is usually given as line and column numbers in a note or error message.
Common execution-time errors include the following:
-
illegal arguments to functions
-
illegal mathematical operations (for example, division by 0)
-
observations in the wrong order for BY- group processing
-
reference to a nonexistent member of an array (occurs when the array's subscript is out of range)
-
open and close errors on SAS data sets and other files in INFILE and FILE statements
-
INPUT statements that do not match the data lines (for example, an INPUT statement in which you list the wrong columns for a variable or fail to indicate that the variable is a character variable).
Out-of-Resources Condition
An execution-time error can also occur when you encounter an out-of-resources condition, such as a full disk, or insufficient memory for a SAS procedure to complete. When these conditions occur, SAS attempts to find resources for current use. For example, SAS may ask the user for permission to delete temporary data sets that might no longer be needed, or to free the memory in which macro variables are stored.
When an out-of-resources condition occurs in a windowing environment, you can use the SAS CLEANUP system option to display a requestor panel that enables you to choose how to resolve the error. When you run SAS in batch, noninteractive , or interactive line mode, the operation of CLEANUP depends on your operating environment. For more information, see the CLEANUP system option in SAS Language Reference: Dictionary , and in the SAS documentation for your operating environment.
Examples
In the following example, an execution-time error occurs when SAS uses data values from the second observation to perform the division operation in the assignment statement. Division by 0 is an illegal mathematical operation and causes an execution-time error.
options linesize=64 nodate pageno=1 pagesize=25; data inventory; input Item $ 1-14 TotalCost 15-20 UnitsOnHand 21-23; UnitCost=TotalCost/UnitsOnHand; datalines; Hammers 440 55 Nylon cord 35 0 Ceiling fans 1155 30 ; proc print data=inventory; format TotalCost dollar8.2 UnitCost dollar8.2; run;
Output 9.5: SAS Log: Execution-Time Error (division by 0)
|
cpu time 0.02 seconds 1 2 options linesize=64 nodate pageno=1 pagesize=25; 3 4 data inventory; 5 input Item $ 1-14 TotalCost 15-20 6 UnitsOnHand 21-23; 7 UnitCost=TotalCost/UnitsOnHand; 8 datalines; NOTE: Division by zero detected at line 12 column 22. RULE:----+----1----+----2----+----3----+----4----+----5----+---- 10 Nylon cord 35 0 Item=Nylon cord TotalCost=35 UnitsOnHand=0 UnitCost=. _ERROR_=1 _N_=2 NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values. Each place is given by: (Number of times) at (Line):(Column). 1 at 12:22 NOTE: The data set WORK.INVENTORY has 3 observations and 4 variables. NOTE: DATA statement used: real time 2.78 seconds cpu time 0.08 seconds 12 ; 13 14 proc print data=inventory; 15 format TotalCost dollar8.2 UnitCost dollar8.2; 16 run; NOTE: There were 3 observations read from the dataset WORK.INVENTORY. NOTE: PROCEDURE PRINT used: real time 2.62 seconds
|
Output 9.6: SAS Listing Output: Execution-Time Error (division by 0)
|
The SAS System 1 Total Units Obs Item Cost OnHand UnitCost 1 Hammers 0.00 55 .00 2 Nylon cord .00 0 . 3 Ceiling fans 55.00 30 .50
|
SAS executes the entire step, assigns a missing value for the variable UnitCost in the output, and writes the following to the SAS log:
-
a note that describes the error
-
the values that are stored in the input buffer
-
the contents of the program data vector at the time the error occurred
-
a note explaining the error.
Note that the values that are listed in the program data vector include the _N_ and _ERROR_ automatic variables. These automatic variables are assigned temporarily to each observation and are not stored with the data set.
In the following example of an execution-time error, the program processes an array and SAS encounters a value of the array's subscript that is out of range. SAS prints an error message to the log and stops processing.
options linesize=64 nodate pageno=1 pagesize=25; data test; array all{*} x1-x3; input I measure; if measure > 0 then all{I} = measure; datalines; 1 1.5 . 3 2 4.5 ; proc print data=test; run;
Output 9.7: SAS Log: Execution-Time Error (subscript out of range)
|
cpu time 0.02 seconds 1 options linesize=64 nodate pageno=1 pagesize=25; 2 3 data test; 4 array all{*} x1-x3; 5 input I measure; 6 if measure > 0 then 7 all{I} = measure; 8 datalines; ERROR: Array subscript out of range at line 12 column 7. RULE:----+----1----+----2----+----3----+----4----+----5----+---- 10 . 3 x1=. x2=. x3=. I=. measure=3 _ERROR_=1 _N_=2 NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 1 observations and 5 variables. NOTE: DATA statement used: real time 0.90 seconds cpu time 0.09 seconds 12 ; 13 14 proc print data=test; 15 run; NOTE: There were 1 observations read from the dataset WORK.TEST. NOTE: PROCEDURE PRINT used: real time 0.81 seconds
|
Data Errors
Data errors occur when some data values are not appropriate for the SAS statements that you have specified in the program. For example, if you define a variable as numeric, but the data value is actually character, SAS generates a data error. SAS detects data errors during program execution and continues to execute the program, and does the following:
-
writes an invalid data note to the SAS log.
-
prints the input line and column numbers that contain the invalid value in the SAS log. Unprintable characters appear in hexadecimal. To help determine column numbers, SAS prints a rule line above the input line.
-
prints the observation under the rule line.
-
sets the automatic variable _ERROR_ to 1 for the current observation.
In this example, a character value in the Number variable results in a data error during program execution:
options linesize=64 nodate pageno=1 pagesize=25; data age; input Name $ Number; datalines; Sue 35 Joe xx Steve 22 ; proc print data=age; run;
The SAS log shows that there is an error in line 8, position 5-6 of the program.
Output 9.8: SAS Log: Data Error
|
cpu time 0.01 seconds 1 2 options linesize=64 nodate pageno=1 pagesize=25; 3 4 data age; 5 input Name $ Number; 6 datalines; NOTE: Invalid data for Number in line 8 5-6. RULE:----+----1----+----2----+----3----+----4----+----5----+----6 8 Joe xx Name=Joe Number=. _ERROR_=1 _N_=2 NOTE: The data set WORK.AGE has 3 observations and 2 variables. NOTE: DATA statement used: real time 0.06 seconds cpu time 0.02 seconds 10 ; 11 12 proc print data=age; 13 run; NOTE: There were 3 observations read from the dataset WORK.AGE. NOTE: PROCEDURE PRINT used: real time 0.01 seconds
|
Output 9.9: SAS Listing Output: Data Error
|
The SAS System 1 Obs Name Number 1 Sue 35 2 Joe . 3 Steve 22
|
You can also use the INVALIDDATA= system option to assign a value to a variable when your program encounters invalid data. For more information, see the INVALIDDATA= system option in SAS Language Reference: Dictionary .
Format Modifiers for Error Reporting
The INPUT statement uses the ? and the ?? format modifiers for error reporting. The format modifiers control the amount of information that is written to the SAS log. Both the ? and the ?? modifiers suppress the invalid data message. However, the ?? modifier also sets the automatic variable _ERROR_ to 0. For example, these two sets of statements are equivalent:
-
input x ?? 10-12;
-
input x ? 10-12;
_error_=0;
In either case, SAS sets the invalid values of X to missing values.
Macro-related Errors
Several types of macro-related errors exist:
-
macro compile time and macro execution-time errors, generated when you use the macro facility itself
-
errors in the SAS code produced by the macro facility.
For more information about macros, see SAS Macro Language: Reference .
[*] When you run SAS in noninteractive mode, more serious errors can cause SAS to enter syntax check mode and stop processing the program.