3

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...

Robert Penridge
  • 8,424
  • 2
  • 34
  • 55
Lovnlust
  • 1,507
  • 3
  • 29
  • 53
  • Have you looked at the stop statement? – InfiniteFlash Mar 28 '16 at 04:05
  • Have you tried `%abort abend` or `%abort return`? – Dominic Comtois Mar 28 '16 at 04:10
  • @DominicComtois these two only stop current session.. not for following ones... Yes I tried .. and they don't work.. – Lovnlust Mar 28 '16 at 06:15
  • @InfiniteFlashChess Could you give me a link to how to use `stop` to achieve that? All I know about stop is to use it inside one data step.. – Lovnlust Mar 28 '16 at 06:18
  • Not sure what you mean by "not for following ones..." -- you want the error checking to impact future sessions?? – Dominic Comtois Mar 28 '16 at 06:19
  • @DominicComtois Yes... If I have 5 programs in one process flow and 2nd program for error checking, when error appears in 2nd program, the 2,3,4,5 will be terminated... – Lovnlust Mar 28 '16 at 06:29
  • Ok so that's what you want, right? If `%abort abend/cancel` doesn't achieve that (I thought it would), then maybe add a condition to the execution of the other programs... automatic macro variable `SYSERR = 0` could be of help. – Dominic Comtois Mar 28 '16 at 06:34
  • Oh, I'm sorry, what I'm thinking about won't be useful. Will have to mull over this. – InfiniteFlash Mar 28 '16 at 06:56
  • May I know how you link the five programs ? If they are linked through '%include' then it is possible to stop the execution of the entire SAS program. – yukclam9 Mar 28 '16 at 10:38
  • Are you using Enterprise Guide? – Joe Mar 28 '16 at 15:35

2 Answers2

2

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.

Joe
  • 62,789
  • 6
  • 49
  • 67
1

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.