DATA Step, Macro, Functions and more

Store value of _ERROR_ in SAS Dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Store value of _ERROR_ in SAS Dataset

I want to store the value of _ERROR_ in a SAS dataset. I have a complete program written, so in case my complete code is not working I need a SAS dataset to be created which will have have value of ERROR = 0 in case there are no errors and ERROR = 1 when there are errors in the program.

 

Thanks


Accepted Solutions
Solution
‎01-19-2016 09:15 AM
Super Contributor
Posts: 340

Re: Store value of _ERROR_ in SAS Dataset

I believe that you are trying to access the log (?)

 


Proc PrintTo Log="D:\xxx.LOG" New; Run;

Data _NULL_;
  x=IntNX('month',2,'01JAN2016'); * d missing;
Run;

Proc PrintTo; Run;

Data Errors (Drop=Line);
  Infile "D:\xxx.LOG" Delimiter='09'x Missover End=Eof;
  Informat Line $200.;
  Input Line;
  Retain MyError 0;
  If Find(UpCase(Line),'_ERROR_=1') Then MyError=1;
  If Eof Then Output;
Run;

View solution in original post


All Replies
Super User
Posts: 7,762

Re: Store value of _ERROR_ in SAS Dataset

Have you already taken a look at the automatic macro variable SYSCC? It might provide what you need.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: Store value of _ERROR_ in SAS Dataset

Posted in reply to KurtBremser

Shall I use syserr option to capture the type of error and map with the error codes?

Super User
Super User
Posts: 7,942

Re: Store value of _ERROR_ in SAS Dataset

Why do you want to do this?  The log is the record of a programs run, most people I know scan the log for findings as error codes may not capture everything.  So my suggestion is use the log, don't try to re-invent it.  The auto error macro variables are useful really only in deciding wether to continue a program or branch somewhere else.

Trusted Advisor
Posts: 1,117

Re: Store value of _ERROR_ in SAS Dataset

Very true, @RW9. There is a lot that can go wrong in a program without SYSCC being set to a value >0, including cases where _ERROR_ was set to 1 (not to speak of logical errors).

 

Example (log):

1    data _null_;
2    a=min(1/max(input('x',1.)+1,0),11);
3    put a 1. b;
4    run;

NOTE: Variable b is uninitialized.
NOTE: Invalid argument to function INPUT at line 2 column 13.
NOTE: Division by zero detected at line 2 column 8.
*.
a=11 b=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 2:26
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 2:8    1 at 2:13
NOTE: At least one W.D format was too small for the number to be printed. The decimal may be shifted by the "BEST" format.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


5    %put &syscc;
0

 

 

That said, SYSCC has at least a chance to provide minimal status information about "a complete program," whereas _ERROR_ doesn't.

Solution
‎01-19-2016 09:15 AM
Super Contributor
Posts: 340

Re: Store value of _ERROR_ in SAS Dataset

I believe that you are trying to access the log (?)

 


Proc PrintTo Log="D:\xxx.LOG" New; Run;

Data _NULL_;
  x=IntNX('month',2,'01JAN2016'); * d missing;
Run;

Proc PrintTo; Run;

Data Errors (Drop=Line);
  Infile "D:\xxx.LOG" Delimiter='09'x Missover End=Eof;
  Informat Line $200.;
  Input Line;
  Retain MyError 0;
  If Find(UpCase(Line),'_ERROR_=1') Then MyError=1;
  If Eof Then Output;
Run;
Super User
Posts: 10,020

Re: Store value of _ERROR_ in SAS Dataset

Why would you want do this ?

 

data x;
 set sashelp.class;
 error=_error_;
 run;
Occasional Contributor
Posts: 12

Re: Store value of _ERROR_ in SAS Dataset

Thanks for the reply.

 

This will not work as I do not want value of _ERROR_ in a dataset. If there are any errors throughout the program (any kind of errors) a new dataset should be created which will have a value of _ERROR_.

 

 

Occasional Contributor
Posts: 12

Re: Store value of _ERROR_ in SAS Dataset

I want to do this because I have connected SAS with other application where user will not get to see the log, they can only see the outputs. So in case there are any errors they will get the conditional display of error message
Super User
Super User
Posts: 7,942

Re: Store value of _ERROR_ in SAS Dataset

Why can they not see the log?  It is a text file which can be saved anywhere.  Use the proc printto, and then either goto the file directly, or send it over as an output file.  

 

The other issue with storing codes is that you end up in a loop.  You have code to capture the code, but what if that has an error, will you capture the code for that, and then the code there...

 

Far easier just to use the log.

Super User
Posts: 7,762

Re: Store value of _ERROR_ in SAS Dataset

It is quite easy to make the logs visible via web browser. Our logs from all the batch jobs are made available for all data warehouse users in this way.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: Store value of _ERROR_ in SAS Dataset

Can I use &syserr option and capture what type of errors we are encountring??
Super User
Posts: 10,020

Re: Store value of _ERROR_ in SAS Dataset

Check function 

SYSMSG()

 

and system macro variables:

 %put &syswarningtext &syserrortext;
Trusted Advisor
Posts: 1,117

Re: Store value of _ERROR_ in SAS Dataset

[ Edited ]

I could imagine that the users of the "application" that @amrshd has "connected SAS with" aren't SAS programmers, but just users of an end-user application. They are not supposed and don't want to look behind the scenes of their GUI and would be unable to interpret the log.

 

So, I think the best way to inform them adequately about the status of their application would be to scan the SAS log and to include various checking steps (e.g. for data issues) in the program and boil down the results to a user-friendly status message in non-technical English (or whatever the most appropriate language is) which is eventually displayed on the GUI.

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 395 views
  • 4 likes
  • 6 in conversation