BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Camp
Fluorite | Level 6

Hi All,

 

Please explain me through a small example that how can we use _Error_ (automatic variable) in our data steps. I am not clear about it.

 

For example, we can use _n_ variable as:

 

data new;
set sashelp.cars;
if _n_ = 100 then output;
run;

 

Similarly, please give me a small example for _error_ variable.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

I am curious about the use of _ERROR_ in the data step program as you are.

 

I wish to supplement to the answer you received earlier.

 

The _error_ variable is set to ZERO before the first iteration of the execution phase of the data step. When some logical or nonsensical error (to SAS) happens, _ERROR_ is set to ONE, irrespective of the number of errors, and the error message with the offending statement(s) are written to the LOG.

 

I just experimented with the following programs.

 

[1]  Simply add _ERROR_ = 1. This is not your question. Simply see what happens.

 

data have;
set sashelp.class;
_error_ = 1;
run;

proc print data = have;
run;

 

The data set HAVE is perfect. But see the LOG. The contents of Program Data Vector(PDV) is dumped to the LOG. No NOTEs.

 

[2] Make a non-sensical Addition.

 

data have;
set sashelp.class;

v = 10 + 'GGGGGG';
run;

 

Again the contents of PDV is dumped to LOG with:

 

NOTE: Invalid numeric data, 'GGGGGG' , at line 481 column 13.

 

The HAVE data set has the new variable (V) with a Missing Value.

 

[3] We now examine whether _ERROR_ can be used in an IF statement as you desired.

 

Before we go into the program, let us wipe out the HAVE data set by:

 

proc delete data = have;
run;

 

Run this program and see what happens as compared previous two programs.

 

data have;
set sashelp.class;

v = 10 + 'GGGGGG';
if _error_ = 1 then stop;
run;

 

Note the HAVE data set has ZERO Observation.

 

While processing the first Observation the invalid Numeric data is found, the message is written to the LOG. Then only, it checks the value of _ERROR_. When it is ONE, the execution of the data step is STOPPED as requested by us.

 

So, what is the message?

You cann't completely stop the message and have to have once anyway. However, LOG messages are shortened unlike in the above two programs.

 

Hope someone may supplement me.

 

After thoughts .....

In the last program the statement

 

if _error_ = 1 then stop;

 

stops after the first iteration of the data step. What if the STOP statement is removed and do something when _ERROR_ is 1?

 

[4] This program compares with [2] above. In [2], the LOG shows a pair of lines, NOTE followed by the dump of the PDV. The following program handles the _ERROR_ = 1 as:

 

 

data have;
set sashelp.class;
v = height + 'GGGGGG';
if _error_ = 1 then do;
*put 'Error Occurred at ' _N_;
v + height;
sum_height + height;
_error_ = 0;
end;
run;

When _error_ = 1  two sum-statements are made. The V becomes a missing value when 'GGGGGG' is added. Subsequently, (v + height) makes v = height. But sum_height works as expected.

 

This time the PDV is not dumped to the LOG. Only the NOTE comes for each observation. So RESETTING _ERROR_ to ZERO stops the dumping of PDV. This is the normal data step functionality. The default output is written when the data step CONTROL reaches the last statement, just before RUN statement. At that time, _ERROR_ happens to be ZERO and PDV is not dumped to LOG.

The final lesson is that PDV dumping to LOG can be avoided by handling _ERROR_.

 

 

View solution in original post

4 REPLIES 4
ballardw
Super User

_Error_ takes values of 1 (there is an error of some type) or 0. This can be from a system related error such as attempting to input a character value into a numeric variable or math such as division by error. So you can test and do any operations that you think likely may help identify or fix the error. Mostly used to print to the log.

 

One difference is that the ERROR function can set the _error_ variable for custom reasons (If Value = "unexpected" then Error "unexpected value for variable Value"; setting the flag and writing a message to log)

Reeza
Super User

Do a proc import on a text file and look at the log. It will have the data step code which includes an example of how the _error_ variable is used. 

KachiM
Rhodochrosite | Level 12

I am curious about the use of _ERROR_ in the data step program as you are.

 

I wish to supplement to the answer you received earlier.

 

The _error_ variable is set to ZERO before the first iteration of the execution phase of the data step. When some logical or nonsensical error (to SAS) happens, _ERROR_ is set to ONE, irrespective of the number of errors, and the error message with the offending statement(s) are written to the LOG.

 

I just experimented with the following programs.

 

[1]  Simply add _ERROR_ = 1. This is not your question. Simply see what happens.

 

data have;
set sashelp.class;
_error_ = 1;
run;

proc print data = have;
run;

 

The data set HAVE is perfect. But see the LOG. The contents of Program Data Vector(PDV) is dumped to the LOG. No NOTEs.

 

[2] Make a non-sensical Addition.

 

data have;
set sashelp.class;

v = 10 + 'GGGGGG';
run;

 

Again the contents of PDV is dumped to LOG with:

 

NOTE: Invalid numeric data, 'GGGGGG' , at line 481 column 13.

 

The HAVE data set has the new variable (V) with a Missing Value.

 

[3] We now examine whether _ERROR_ can be used in an IF statement as you desired.

 

Before we go into the program, let us wipe out the HAVE data set by:

 

proc delete data = have;
run;

 

Run this program and see what happens as compared previous two programs.

 

data have;
set sashelp.class;

v = 10 + 'GGGGGG';
if _error_ = 1 then stop;
run;

 

Note the HAVE data set has ZERO Observation.

 

While processing the first Observation the invalid Numeric data is found, the message is written to the LOG. Then only, it checks the value of _ERROR_. When it is ONE, the execution of the data step is STOPPED as requested by us.

 

So, what is the message?

You cann't completely stop the message and have to have once anyway. However, LOG messages are shortened unlike in the above two programs.

 

Hope someone may supplement me.

 

After thoughts .....

In the last program the statement

 

if _error_ = 1 then stop;

 

stops after the first iteration of the data step. What if the STOP statement is removed and do something when _ERROR_ is 1?

 

[4] This program compares with [2] above. In [2], the LOG shows a pair of lines, NOTE followed by the dump of the PDV. The following program handles the _ERROR_ = 1 as:

 

 

data have;
set sashelp.class;
v = height + 'GGGGGG';
if _error_ = 1 then do;
*put 'Error Occurred at ' _N_;
v + height;
sum_height + height;
_error_ = 0;
end;
run;

When _error_ = 1  two sum-statements are made. The V becomes a missing value when 'GGGGGG' is added. Subsequently, (v + height) makes v = height. But sum_height works as expected.

 

This time the PDV is not dumped to the LOG. Only the NOTE comes for each observation. So RESETTING _ERROR_ to ZERO stops the dumping of PDV. This is the normal data step functionality. The default output is written when the data step CONTROL reaches the last statement, just before RUN statement. At that time, _ERROR_ happens to be ZERO and PDV is not dumped to LOG.

The final lesson is that PDV dumping to LOG can be avoided by handling _ERROR_.

 

 

tmoorman
Calcite | Level 5

This may be useful to someone. I was looking to control SAS processing, based on the value of _ERROR_. If the value of error can be converted into a macro variable, then this becomes possible.  The code is as follows:

 

proc delete data = have;
run;

 

data have;
set sashelp.class;
call symputx(' error ', _error_);
run;

 

%put &error;

 

proc delete data = have;
run;

 

data have;
set sashelp.class;
v = 10 + 'GGGGGG';
call symputx(' error ', _error_);
run;

 

%put &error;

 

proc delete data = have;
run;

 

data have;
set sashelp.class;
call symputx(' error ', _error_);
run;

 

%put &error;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 11358 views
  • 7 likes
  • 5 in conversation