Help using Base SAS procedures

A question about _error_ automatic variable

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

A question about _error_ automatic variable

Hi all,

I have been struggling with a stupid question for four hours…I need your help…

It is about the automatic variable _error_. I actually want to use this variable to do debug things. However, I found a weird phenomenon…The procedure did have error and steps were failed to run in my program, however the _error_ was still resolved to be 0, which was different from my thought, 1 .

For example, give you the code as follow

data test;

input x y;

cards;

1 2

3 4

;

data test;

set test;

n=_n_

%put &_error_;

run;

The _error_ was resolved as o in the log window…

106421  data test;

106422  input x y;

106423  cards;

NOTE: The data set WORK.TEST has 2 observations and 2 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

106426  ;

106427

106428  data test;

106429  set test;

106430  n=_n_

106431  %put &_error_;

0

106432  run;

        ---

        22

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=,

              <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR,

              ^=, |, ||, ~=.

Could you teach me how to use _error_ in debugging? Thank you.


Accepted Solutions
Solution
‎12-10-2014 06:05 PM
Super User
Super User
Posts: 6,500

Re: A question about _error_ automatic variable

SAS will set SYSERR macro variable for some types of errors.  But if you want to convert the data step _ERROR_ variable into a macro variable then add code in your data step to do that.  For example the step below will not cause the automatic macro variable SYSERR to be set, but the CALL SYMPUTX will set the user macro variable _ERROR_.

%let _error_=0;

data y;

   input x 3. ;

   if _error_ then call symputx('_error_','1');

cards;

abc

run;

%put syserr=&syserr _error_=&_error_ ;

View solution in original post


All Replies
Super User
Posts: 17,823

Re: A question about _error_ automatic variable

You're missing a semi-colon after _n_, before the put statement.

_error_ is an automatic variable NOT a macro variable.  The following code works, but I'm not sure what you're looking for it to do.

data test;

input x y;

cards;

1 2

3 4

;

data test;

set test;

n=_n_;

put _error_;

run;

Contributor
Posts: 61

Re: A question about _error_ automatic variable


Hi Reeza,

Thank you for your reply. Actually, I was intentionally missing the the semi-colon after _n_ to make the mistake. Beasue I want to check whether the _errror_ would be changed to 1 in that case. Aftet test, in my example, even through the procedure was stopped because of the error, the _error_ was still showing as 0 in the log file. That was my question, why was not it 1?

In addition, in your revised code, I do not see the PUT statement works in the log file....

Respected Advisor
Posts: 3,124

Re: A question about _error_ automatic variable

_error_ is a PDV variable that only comes alive when a data step is executed. The error in your example (missing a semicolon) is a syntax error that will be detected in the compiling stage, meaning when this error happens, there is no PDV yet, so it is meaningless to talk about a PDV variable, such as _error_.

Not sure about your agenda, but if your purpose is debugging SAS code, then there are many options that you can use, to start, check this link:

SAS(R) 9.3 Language Reference: Concepts, Second Edition

Thanks,

Haikuo

Contributor
Posts: 61

Re: A question about _error_ automatic variable

Hi Hai.kuo,

Thank you very much for your explain! Your link is very helpful. Thank you.

Contributor
Posts: 61

Re: A question about _error_ automatic variable

By the way, yes, I want to do debugging. It really takes me a lot of time to search for debugging methods. Thanks.

Super User
Posts: 17,823

Re: A question about _error_ automatic variable

What kind of debugging? Automated debugging for programs or debugging techniques for interactive programming?

There's a ton of papers on lexjansen.com with regards to this. 

Solution
‎12-10-2014 06:05 PM
Super User
Super User
Posts: 6,500

Re: A question about _error_ automatic variable

SAS will set SYSERR macro variable for some types of errors.  But if you want to convert the data step _ERROR_ variable into a macro variable then add code in your data step to do that.  For example the step below will not cause the automatic macro variable SYSERR to be set, but the CALL SYMPUTX will set the user macro variable _ERROR_.

%let _error_=0;

data y;

   input x 3. ;

   if _error_ then call symputx('_error_','1');

cards;

abc

run;

%put syserr=&syserr _error_=&_error_ ;

Contributor
Posts: 61

Re: A question about _error_ automatic variable

Thank you very much for your explain and the code. Thanks a lot!!

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 455 views
  • 6 likes
  • 4 in conversation