DATA Step, Macro, Functions and more

_Error_

Reply
Frequent Contributor
Posts: 100

_Error_

Greetings:

Another beginner question. Why does SAS retain _Error_ (the automatic variable) value at the end of a data step processing?

Here is the wording on my big SAS book (SAS Certification Prep Guide). '...the value of _N_ is set to 2 and control returns to the top of the Data step. Finally, the variable values in PDV are re-set to missing. Notice that the automatic variable _Error_ retains its value.' I thought it has to reset _Error_ to 0 for the next cycle of execution?

Thank you for your time!
Regular Contributor
Posts: 187

Re: _Error_

_error_ is retained because SAS programmers programmed it that way.
Valued Guide
Posts: 634

Re: _Error_

The _ERROR_ variable allows us to check for errors across the execution of the step. Since we have the ability to query its value whenever we want (and to reset its value), we can detect errors at either the row level or at the level of execution across the entire step.
Super User
Posts: 10,020

Re: _Error_

Hi.
I think that _Error_ retains its value is because of _Error_ is system variable not as the temporary variable which will reset missing at next data step .
_Error_ is just like _n_ which is also retain its value at next data step.


Ksharp
Super Contributor
Super Contributor
Posts: 3,174

Re: _Error_

I have to disagree with Ksharp, the behavior with _N_ is not consistent with _ERROR_ .

And also to seek clarification from the OP, as I don't see where the value of _ERROR_ is actually "retained" across DATA step iterations, as its value is reset to zero.

When a DATA step returns to the top, the _N_ is reset to the next data-value and does not "retain" the prior iteration value, not even before a SET is executed.

And, in the case of _ERROR_, SAS also does not retain the prior DATA step iteration value.

A code-snippet is listed below this post to demonstrate - for those who want to execute it on their own SAS system.

Scott Barry
SBBWorks, Inc.

data x;
do a=1 to 4;
output;
end;
stop;
run;
DATA _null_;
PUTLOG '>BEFORE-SET>' / _ALL_;
set x;
if a = 2 then x = input('a',best.);
PUTLOG '>AFTER-SET>' / _ALL_;
RUN;
Valued Guide
Posts: 634

Re: _Error_

As Scott points out the temporary variable _ERROR_'s value does persist beyond the DATA step boundary.
Super Contributor
Super Contributor
Posts: 3,174

Re: _Error_

The value of _ERROR_ is only maintained until it is reset by the user, influenced/assigned based on another INPUT function or use of an INPUT statement with a suitable INFORMAT.

However, when you return to the top of a DATA step execution, the value is reset to _ERROR_=0 -- it's prior step-execution value is not maintained.

This behavior is inherent to the SAS system architecture.

Scott Barry
SBBWorks, Inc. Message was edited by: sbb
Valued Guide
Posts: 634

Re: _Error_

I should have mentioned that the automatic macro variable &SYSERR does persist across step boundaries, and can be used to evaluate the success of a step at the step level.
Frequent Contributor
Posts: 100

Re: _Error_

Thanks everyone! Scott's testing codes are very educational. I now know how to check the values in PDV (yes, a real step for a beginner). All the abstract talk about data step iterations now makes a little more sense to me, as I could see the action in snapshots. Also thanks for the &SYSERR tip! I didn't know it exists at all.
Super Contributor
Super Contributor
Posts: 3,174

Re: _Error_

To ArtC: From the SAS DOC (link below), there is indication that the SYSERR macro variable *IS* reset at the step-boundary.

SAS 9.2 Macro Language: Automatic Macro Variables, SYSERR Automatic Macro Variable
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000208995.htm

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 634

Re: _Error_

Thank you Scott for reminding me that I need to remember to be more precise in what I write. :-) The &SYSERR is reset at the start of a step. This means that it can be used/checked after a step (after RUN or QUIT), until it is reset for the next step. Which takes place once the step threshold has been crossed (PROC or DATA statement).
[pre]proc sort data=abc;
by x;
run;
%put &syserr;
data new;%put &syserr;
set sashelp.class;
run;[/pre]
Ask a Question
Discussion stats
  • 10 replies
  • 243 views
  • 0 likes
  • 5 in conversation