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.
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 might not be valid for a particular usage
compile or execution 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 SAS continues processing your program based on its assumptions. If SAS cannot correct the error, it prints an error message to the log. If you do not want SAS to correct syntax errors, you can set the NOAUTOCORRECT system option. For more information, see the AUTOCORRECT system option in the SAS System Options: Reference.
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;
SAS Log: Syntax Error (Misspelled Key Word)
39   date temp;
     ----
     14
WARNING 14-169: Assuming the symbol DATA was misspelled as date.

40      x=1;
41   run;
NOTE: The data set WORK.TEMP has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
42   
43   proc print data=temp;
44   run;
NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
45   proc printto; run;
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. This is 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;
SAS Log: Syntax Error (Missing Semicolon)
67   data temp
68      x=1;
         -
         22
         76
ERROR 22-322: Syntax error, expecting one of the following: a name, 
              a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.  

ERROR 76-322: Syntax error, statement will be ignored.

69   run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      
70   
71   proc print data=temp;
72   run;
NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
73   proc printto; run;
Whether subsequent steps are executed depends on which method of running SAS you use, as well as on your operating environment.
Note: You can add these lines to your code to fix unmatched comment tags, unmatched quotation marks, and missing semicolons:
/* '; * "; */;
quit;
run;

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.)
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 at compile time.
data _null_;    
   array all{*} x1-x5;    
   all=3;
   datalines; 
1 1.5 
. 3
2 4.5
3 2 7
3 . .
;   

run;
SAS Log: Semantic Error (Illegal Reference to an Array)
81   data _null_;
82      array all{*} x1-x5;
ERROR: Illegal reference to the array all.
83      all=3;
84      datalines;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.15 seconds
      cpu time            0.01 seconds
      
90   ;
91   
92   run;
93   proc printto; run;
The following is another example of a semantic error that occurs at compile time. In this DATA step, the libref SOMELIB has not been previously assigned in a LIBNAME statement.
data test;
   set somelib.old;
run;
SAS Log: Semantic Error (Libref Not Previously Assigned)
101  data test;
ERROR: Libname SOMELIB is not assigned.
102     set somelib.old;
103  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.

An example of a semantic error that occurs at execution time is when a "NOTE: SAS went to a new line when input statement reached past the end of a line." is output. This note is written to the SAS log when FLOWOVER is used and all the variables in the INPUT statement cannot be fully read.

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. (footnote1) 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 might ask the user for permission to perform these actions in out-of-resource conditions:
  • Delete temporary data sets that might no longer be needed.
  • 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. The requestor panel 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 System Options: Reference, 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.
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;
SAS Log: Execution-Time Error (division by 0)
115  data inventory;
116     input Item $ 1-14 TotalCost 15-20
117           UnitsOnHand 21-23;
118     UnitCost=TotalCost/UnitsOnHand;
119     datalines;
NOTE: Division by zero detected at line 118 column 22.
RULE:      ----+----1----+----2----+----3----+----4----+----5---
121        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 118:22   
NOTE: The data set WORK.INVENTORY has 3 observations and 4 
      variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
      
123  ;
124  
125  proc print data=inventory;
126     format TotalCost dollar8.2 UnitCost dollar8.2;
127  run;

NOTE: Writing HTML Body file: sashtml1.htm
NOTE: There were 3 observations read from the data set 
      WORK.INVENTORY.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.56 seconds
      cpu time            0.01 seconds
      
SAS Output: Execution-Time Error (division by 0)
SAS Listing Output: Execution-Time Error (Division by 0)
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.
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;
SAS Log: Execution-Time Error (Subscript Out of Range)

163  data test;
164     array all{*} x1-x3;
165     input I measure;
166     if measure > 0 then
167        all{I} = measure;
168     datalines;
ERROR: Array subscript out of range at line 167 column 7.
RULE:      ----+----1----+----2----+----3----+----4----+----5---
170        . 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.
WARNING: Data set WORK.TEST was not replaced because this step 
         was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
172  ;
173  
174  proc print data=test;
175  run;
NOTE: No variables in data set WORK.TEST.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
176  proc printto; run;

Data Errors

Definition

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:
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.
SAS Log: Data Error
234  data age;
235     input Name $ Number;
236     datalines;
NOTE: Invalid data for Number in line 238 5-6.
RULE:      ----+----1----+----2----+----3----+----4----+----5---
238        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 (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      
240  ;
241  
242  proc print data=age;
243  run;

NOTE: Writing HTML Body file: sashtml2.htm
NOTE: There were 3 observations read from the data set WORK.AGE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.07 seconds
      cpu time            0.04 seconds

SAS Output: Data Error
SAS Listing Output: Data Error
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 System Options: Reference.

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.
FOOTNOTE 1:When you run SAS in noninteractive mode, more serious errors can cause SAS to enter syntax check mode and stop processing the program.[return]