Lovnlust
Lovnlust

Reputation: 1537

Terminate whole process flow instead of single program

Is there a way to stop the process if certain criteria in any of the programs in this process is met?

I have a process consist of 5 SAS programs. This process is scheduled to run at 8am every morning. However, sometimes the database is not refreshed and this process will send out weird figures.

I need to have "exception control".. In 2nd program I check the database with some criteria. If no error, then keep running the rest of the code. Otherwise, send out an notification email and STOP running the 2nd program and all the subsequent programs.

I try %abort cancel but it only terminate the current program. The subsequent programs are not affected.. I can do checking in every single program but that make the code redundant...

I also try google "terminate SAS process" but most of them refer to abort statements which doesn't help...

Upvotes: 3

Views: 1775

Answers (2)

Leonid Batkhan
Leonid Batkhan

Reputation: 101

My understanding is that this is a batch process. You don't specify Operating System you are running your process on. Let's suppose you are running it on UNIX/Linux (I am hoping it is similar on Windows). Let's assume that your 5-programm process is run by the following shell script: sas /program1.sas sas /program2.sas sas /program3.sas sas /program4.sas sas /program5.sas

If you want to stop your remaining process after program2.sas completes with ERRORs or WARNINGs you can modify your script to be

sas /program1.sas
sas /program2.sas
if [ $? -ne 0 ]
   then
      exit
fi
sas /program3.sas
sas /program4.sas
sas /program5.sas

In this script code a special shell script variable $? status code is passed from the previous command from SAS (0 means successful completion). If it is not 0 then the whole script stops due to the exit command.

For more information and code examples see How to conditionally terminate a SAS batch flow process in UNIX/Linux SAS blog post.

Upvotes: 1

Joe
Joe

Reputation: 63424

If you're using Enterprise Guide, this is built into the program via logic gates.

First, in the program that determines whether the database file passed/failed ("gate program"), assign a macro variable a value based on that test. Presumably this program will do only things you're happy for it to do even if it fails.

On the process flow page, you right click on the program that determines if the database file passed/failed, and select 'Condition -> Add'.

Then add a condition based on a Macro Variable, and use 'equals' and the value you're looking for (or 'greater than' or whatever makes sense). Then select the next task after "Then run this task"; and put the other option after Else run this task.

Then, whichever of the two is forward-moving, should then have links to the rest of the programs you want run; the one that's not should end the process.

SAS gives an example of how to do that in KB Sample 39995 including a sample project you can download.


Second, you can set OBS=0 if you reach the error condition. This will let SAS continue working, but it in most cases won't be able to do anything (since OBS=0, then it can only affect 0 records of any dataset). I'm not sure that's a guarantee that it won't do anything, but in everything I've done that's been sufficient. I also have used OPTIONS ERRORABEND which works fine if you do all of your processing with external libnames which won't automatically reconnect when SAS is reconnected.

Upvotes: 2

Related Questions