DATA Step, Macro, Functions and more

Automatic variable _Error_

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Automatic variable _Error_

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!


Accepted Solutions
Solution
‎07-17-2016 01:58 PM
Super Contributor
Posts: 257

Re: Automatic variable _Error_

[ Edited ]

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


All Replies
Super User
Posts: 10,538

Re: Automatic variable _Error_

_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)

Super User
Posts: 17,907

Re: Automatic variable _Error_

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. 

Solution
‎07-17-2016 01:58 PM
Super Contributor
Posts: 257

Re: Automatic variable _Error_

[ Edited ]

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_.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1201 views
  • 6 likes
  • 4 in conversation