BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
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!
11 REPLIES 11
bheinsius
Lapis Lazuli | Level 10
_error_ is retained because SAS programmers programmed it that way.
ArtC
Rhodochrosite | Level 12
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.
Ksharp
Super User
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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;
ArtC
Rhodochrosite | Level 12
As Scott points out the temporary variable _ERROR_'s value does persist beyond the DATA step boundary.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
ArtC
Rhodochrosite | Level 12
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.
mnew
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
ArtC
Rhodochrosite | Level 12
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]
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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 11 replies
  • 2199 views
  • 0 likes
  • 6 in conversation