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

I am wondering why the above referenced macro variable does not set to 1 when it encounters an error and stops processing as in the sample code below. I have deliberately set the filename statement to a non-existent URL. How do I use the _EFIERR_ macro variable in subsequent code when it will not return a value of 1 when SAS processing is stopped? Thank you.

filename _000002 url "%str(xyxp://www.bxb.cats/us/Find-Business-Reviews/name/ALEX+FIGLIOLIA+WATER+SEWER/11215)" debug;

data f nf;
format webpage $32767.;
infile _000002 lrecl=32767 delimiter="><";
input webpage $ @@;
if (prxmatch("/Search Result here!|a href=/i",webpage)) then output f;
if (prxmatch("/did not match any Business Names/i",webpage)) then output nf;
if _ERROR_ then call symput('_EFIERR_',1);
run;

data _null_;
e1=&_EFIERR_;
put e1;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
jakarman
Barite | Level 11

Think in the other way. As the datastep kan fail and never run it is bad idea trying to set as fail indicator.

It is more reliable and sure code to put a value in when everything has processed well as good code will run to the expected end.

So:

%let errstp_stat=1 ;  /* to begin with */         

..

Whatever you run 

  call symput("errstp_stat",0) ; /* on the location when you processing is done ok */

..

%macro err_chk ;

   /* do your proceeding tests, abort or whatever you like. */

%mend;

%err_chk;

This construction Di programmers will recognize when seen the generated code.

---->-- ja karman --<-----

View solution in original post

8 REPLIES 8
Amir
PROC Star

Hi,

Try displaying the value of _ERROR_ in the first data step to make sure it is non zero.

Also, maybe try adding an else to your if statement to assign 0 (zero) to macro variable _EFIERR_ so that it always has a value assigned to it.

Otherwise post the log if it is still not making sense.

Regards,

Amir.

BPrater_cancapital_com
Calcite | Level 5

Thanks.

I tried making the following alteration, but the value of _ERROR_ is never output since the original data step never iterates. Both logs are attached. I guess the basic question is how to understand that a data step has failed and pass that information to subsequent code? The only other thing I can think of is to proc printto the log for that one data step and then scan the log with a prxmatch.

filename _000002 url "%str(xyxp://www.bxb.cats/us/Find-Business-Reviews/name/ALEX+FIGLIOLIA+WATER+SEWER/11215)" debug;

%let _EFIERR_=0;

data f nf;

format webpage $32767.;

infile _000002 lrecl=32767 delimiter="><";

input webpage $ @@;

if (prxmatch("/Search Result here!|a href=/i",webpage)) then output f;

if (prxmatch("/did not match any Business Names/i",webpage)) then output nf;

err=_ERROR_; call symput("err",err);

if _ERROR_ then call symput("_EFIERR_",1);

run;

data _null_;

err='&err';

e1=&_EFIERR_;

put e1;

put err;

run;

Astounding
PROC Star

To display the macro variables properly, get rid of the final DATA step.  Just write them to the log using:

%put _user_;

Better yet, write values within the first DATA step just before the final RUN statement:

put _all_;

To get a DATA step to keep on running after an error occurs, it sometimes works to reset _ERROR_ yourself.  For example, just before the RUN statement in the first DATA step, add:

_error_=0;

No guarantees as to what results you'll see on that, but it might become a part of the solution.

BPrater_cancapital_com
Calcite | Level 5

In your advice, I did find the SYSERRORTEXT to be of some assistance. The value is blank when the data step runs without error; contains text when the data step fails.

The only problem I found is that it is a read-only attribute so is resistant to manual reset. Any thoughts on that?

Thanks.

Astounding
PROC Star

I think I have seen cases where _EFIERR_ is a reserved name in a DATA step.  Just to be safe, change the last line from this:

if _ERROR_ then call symput('_EFIERR_', 1);

To these two lines:

if _ERROR_ then call symputx('URL_NotFound', 1);

_error_=0;

Note that CALL SYMPUTX will perform the numeric to character conversion without using leading blanks, and without generating a conversion message on the log.

Should be a little more to work with at that point ...

jakarman
Barite | Level 11

Think in the other way. As the datastep kan fail and never run it is bad idea trying to set as fail indicator.

It is more reliable and sure code to put a value in when everything has processed well as good code will run to the expected end.

So:

%let errstp_stat=1 ;  /* to begin with */         

..

Whatever you run 

  call symput("errstp_stat",0) ; /* on the location when you processing is done ok */

..

%macro err_chk ;

   /* do your proceeding tests, abort or whatever you like. */

%mend;

%err_chk;

This construction Di programmers will recognize when seen the generated code.

---->-- ja karman --<-----
BPrater_cancapital_com
Calcite | Level 5

Thank you Mr Karman. Reversing my thinking was the exact solution. A failed data step can never be relied upon as an indicator of success.

snoopy369
Barite | Level 11

_ERROR_ is not quite what you think it is.  _ERROR_ does not indicate an error that causes the data step to terminate; that wouldn't be checkable except by exclusion (like Jaap suggests).  IE, the moment a SAS data step finds an error that causes it to terminate, it terminates - it doesn't go to the bottom and see if it should do something about it.  There's no TRY/CATCH in SAS.

_ERROR_ indicates data errors, such as division by zero, invalid data provided to a PUT or INPUT, etc.  See SAS(R) 9.2 Language Reference: Concepts, Second Edition for more details.  PROC IMPORT for CSV, where you probably first saw this variable, uses this to identify issues with data formatting (such as if a numeric column contains character values).

If you want to check for actual termination errors, then check &SYSERR, which will be 0 if the last data step worked or nonzero if it failed.  See SAS(R) 9.2 Macro Language: Reference for more details and be aware it's not supported by all procs.

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
  • 8 replies
  • 5745 views
  • 3 likes
  • 5 in conversation