Showing of SAS errors

Doug Hemken

Jul 2017

To see SAS errors, use chunk option "error=TRUE".

SAS does not always exit with an error when it encounters problems, but when it does, you should see the log in your document instead of the code.

First, set up your SAS engine configuration.

require(SASmarkdown)
if (file.exists("C:/Program Files/SASHome/SASFoundation/9.4/sas.exe")) {
  saspath <- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
} else {
  saspath <- "sas"
}
sasopts <- "-nosplash -ls 75"
sas_collectcode()

A Semantic Error.

Semantic errors (typos etc.) cause SAS to return an error status when SAS finishes. Unless you tell R to allow errors, this aborts processing your document.

In this example, try to use WORK data, without setting up data there. SAS produces an ERROR. (No output is produced.)

Use chunk option "error=TRUE".

```{r procmeans, engine="sas", engine.path=saspath, engine.opts=sasopts, comment="", error=TRUE}
proc means data=class;
run;
```

Which produces:

2          proc means data=class;
ERROR: File WORK.CLASS.DATA does not exist.
3          run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

ERROR: Errors printed on page 1.

An Execution Error.

Division by zero produces error flags within the DATA step, but does not cause SAS to return an error code when it finishes. So even if we allow for errors, the "sas" engine gives us the usual output.

data class;
    set sashelp.class(obs=5);
    age0 = age/0;
    keep age age0;
    run;

proc print data=class;
run;
                            Obs    Age    age0

                             1      14      . 
                             2      13      . 
                             3      13      . 
                             4      14      . 
                             5      14      . 

To see the DATA step ERROR, switch to the "saslog" engine. Here the chunk option "error" is irrelevant.

```{r divzero, engine="saslog", engine.path=saspath, engine.opts=sasopts, comment="", error=TRUE}
```

Producing both the log and the PROC PRINT output.

2          data class;
3              set sashelp.class(obs=5);
4              age0 = age/0;
5              keep age age0;
6              run;

NOTE: Division by zero detected at line 4 column 15.
Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 age0=. _ERROR_=1 _N_=1
NOTE: Division by zero detected at line 4 column 15.
Name=Alice Sex=F Age=13 Height=56.5 Weight=84 age0=. _ERROR_=1 _N_=2
NOTE: Division by zero detected at line 4 column 15.
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98 age0=. _ERROR_=1 _N_=3
NOTE: Division by zero detected at line 4 column 15.
Name=Carol Sex=F Age=14 Height=62.8 Weight=102.5 age0=. _ERROR_=1 _N_=4
NOTE: Division by zero detected at line 4 column 15.
Name=Henry Sex=M Age=14 Height=63.5 Weight=102.5 age0=. _ERROR_=1 _N_=5
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).
      5 at 4:15   
NOTE: There were 5 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 5 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
      

7          
8          proc print data=class;
9          run;

NOTE: There were 5 observations read from the data set WORK.CLASS.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.09 seconds
      cpu time            0.06 seconds
      
                            Obs    Age    age0

                             1      14      . 
                             2      13      . 
                             3      13      . 
                             4      14      . 
                             5      14      . 

Replicate in HTML.

The semantic error, where we need chunk option "error=TRUE". In this particular example, the sas and sashtml engines give us the same result, because there is no output produced by either.

6          proc means data=class;
ERROR: File WORK.CLASS.DATA does not exist.
7          run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

ERROR: Errors printed on page 1.

And the execution error, where the chunk option is irrelevant. Using the sashtmllog engine, we see both the DATA step errors and the HTML output.

6          data class;
7              set sashelp.class(obs=5);
8              age0 = age/0;
9              keep age age0;
10             run;

NOTE: Division by zero detected at line 8 column 15.
Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 age0=. _ERROR_=1 _N_=1
NOTE: Division by zero detected at line 8 column 15.
Name=Alice Sex=F Age=13 Height=56.5 Weight=84 age0=. _ERROR_=1 _N_=2
NOTE: Division by zero detected at line 8 column 15.
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98 age0=. _ERROR_=1 _N_=3
NOTE: Division by zero detected at line 8 column 15.
Name=Carol Sex=F Age=14 Height=62.8 Weight=102.5 age0=. _ERROR_=1 _N_=4
NOTE: Division by zero detected at line 8 column 15.
Name=Henry Sex=M Age=14 Height=63.5 Weight=102.5 age0=. _ERROR_=1 _N_=5
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).
      5 at 8:15   
NOTE: There were 5 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 5 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds
      

11         
12         proc print data=class;
13         run;

NOTE: There were 5 observations read from the data set WORK.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
      
Obs Age age0
1 14 .
2 13 .
3 13 .
4 14 .
5 14 .