BookmarkSubscribeRSS Feed
LuGa
Obsidian | Level 7

Dear All,

 

I've identified a potential risk of unreliable results when using SAS for analysis. This risk stems from the fact that an ERROR in SAS doesn't necessarily halt the program's execution. For instance, the program may continue running even if it encounters issues such as returning empty datasets, failing to process SQL clauses, or not updating datasets as expected.

 

Manually reviewing the log after a program has run is a crucial step, but it's not foolproof and could lead to erroneous results slipping through.

 

I'm interested in exploring how we might alter SAS's error handling procedure to align more closely with typical programming language behavior. Specifically, I'm seeking a solution where encountering an error would stop program execution, similar to how errors are handled in other programming languages.

 

I want to clarify that I'm not looking for options like ERRORABEND, which would abruptly close the entire SAS session, nor am I seeking ways to suppress error messages from appearing in the log.

 

Your insights and suggestions on this matter would be greatly appreciated.

 

Best regards,

Lukas

9 REPLIES 9
sbxkoenk
SAS Super FREQ
proc options group=errorhandling;
run;

Koen

sbxkoenk
SAS Super FREQ

How to conditionally stop SAS code execution and gracefully terminate SAS session?
By Leonid Batkhan on SAS Users March 24, 2021
https://blogs.sas.com/content/sgf/2021/03/24/how-to-conditionally-stop-sas-code-execution-and-gracef...

 

How to conditionally terminate a SAS batch flow process in UNIX/Linux?
By Leonid Batkhan on SAS Users March 5, 2019
https://blogs.sas.com/content/sgf/2019/03/05/how-to-conditionally-terminate-a-sas-batch-flow-process...

 

LuGa
Obsidian | Level 7

1.) Would require to add one of the statements in the blogpost after each calculation step.

 

2.) Assumes the same as above, but as a bash script.

 

Both steps would not bring the expected result. 

 

To clarify: I am expecting a default solution, which stops a SAS script from running if any of the submitted procedures produces an error (or a warning).

 

is it possible to wrap a while loop around the entire program?

 

BR
Lukas

 

LuGa
Obsidian | Level 7
Hello Koen,

thanks for your prompt help.

Above line does not bring the expected result.

SAS still ignores multiple ERROR statements and executes procedures on empty data objects.

BR
Lukas
sbxkoenk
SAS Super FREQ

@LuGa wrote:
SAS still ignores multiple ERROR statements and executes procedures on empty data objects.

Empty data objects ????

You mean SAS activates OBS=0 option and puts itself in syntax-check modus when a critical error is met?

 

You don't like that behavior?
Why don't you enable ERRORABEND option?

 

You can also play with (no)replace option or repempty= data set option.
REPEMPTY= Data Set Option --> Specifies whether a new, empty data set can overwrite an existing SAS data set.

You can also work with %GOTO EXIT; like here :
SAS® 9.4 and SAS® Viya® 3.5 Programming Documentation
Example: Providing Exits in a Large Macro
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0jfeh75p72icen1ddd9una5zbmm.htm

 

Koen

Tom
Super User Tom
Super User

Did you check the meaning of all of the options in the ERRORHANDLING group?

Did you try setting the ERRORABEND option?

 

If there are situations that you consider an "error" but SAS does not (for example model does not fit) then add your own logic at that point to issue an ABORT ABEND command.

SASKiwi
PROC Star

To clear, there is no current SAS option to halt program execution when an error is found. Such an option has long been requested in SAS Product Suggestions. That means you are left with ERRORABEND and SYNTAXCHECK. Personally I find the latter most useful and it is the default for batch jobs anyway. You may find it is a good compromise given the lack of a halt system option.

WarrenKuhfeld
Ammonite | Level 13

This doesn't really answer your question, but here are some notes.

 

Some procedures are interactive. You can submit multiple groups of statements within a procedure for multiple results. Such procedures by design do not stop after an error.

 

Most procedures by design do not stop if there is a data error in a BY group.

 

I was a procedure and macro developer at SAS for many years. In my macros, I would put statements like this at the end of each step:

%if &syserr %then %goto %abort;

That way I could stop my macro if something bad happened internally, but it was not always guaranteed to work for reasons cited above.

Quentin
Super User

SAS error handling is a bit of a mash-up, but you can typically get it to do something reasonable.

 

Can you describe more of your situation?  It sounds like you are running a script interactively? Are you using PC SAS, Enterprise Guide, or Studio?  While it would be nice to have one solution, error handling in interactive sessions vs batch sessions is different.  And some IDEs set different error handling options.

 

Can you post an example with three or four little steps that generates an error and does not exit in a way that you would like?

 

Depending on your settings, when SAS detects an error, it will enter "syntax check mode".  This does continue to fill the log, but it doesn't actually execute the code because it sets obs=0.  So it's not exactly what you want, but close.

 

There is also the issue of what problems SAS considers to be an error vs warning etc.

 

So if you can share an example, we can provide more suggestions. 

For an example like:

data a ;
  set sashelp.class ;
run ;

data foo ;
  make an error;
run ;

data b ;
 set a ;
run ;

If I run it in PC SAS interactively, the third step will run, despite the errors in the second step.  But if I set the system option DMSSYNCHK, SAS will enter syntax check mode when the error occurs.  The third step will be compiled but it won't actually replace WORK.B.  The log is:

14   options dmssynchk ;
15
16   data a ;
17     set sashelp.class ;
18   run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.A has 19 observations and 5 variables.

19
20   data foo ;
21     make an error;
       ----
       180
ERROR 180-322: Statement is not valid or it is used out of proper order.

22   run ;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.FOO may be incomplete.  When this step was stopped there were 0 observations and 0 variables.
WARNING: Data set WORK.FOO was not replaced because this step was stopped.

23
24   data b ;
25    set a ;
26   run ;

NOTE: The data set WORK.B has 0 observations and 5 variables.
WARNING: Data set WORK.B was not replaced because this step was stopped.



The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1298 views
  • 6 likes
  • 6 in conversation