Hi, my question is 'How to completely stop a SAS batch while running?' I read through the posts available on this website however, using CTRL+BRK does only stop the current SAS program within the batch. But the next SAS program after that one still continues to run. I am looking for a procedure to bring the batch to a complete stop so that SAS terminates.
So, let's say I have 3 SAS programs under the batch and when I start the batch the first programs runs and then when 1st completed then 2nd program starts and so on. When I stop the 2nd program running, yes, the 2nd one is killed however, then goes to the 3rd SAS program and that starts running. My goal is when I stop the batch, I don't want the rest of the programs run.
Any ideas, suggestions are appreciated.
Thanks
A batch job runs unattended, so there is no interface from SAS to kill it like you can with an interactive job. But, if you have access, typically the operating system provides ways to kill an active job.
Where/how is your batch submitted job running? Is it running locally on a Windows laptop? Then you can use Task Manager to find the running process and kill it. If it's running on a linux server and you can connect via a terminal, then you can use linux commands to find the process and kill it.
Thanks @Quentin for your response. The problem is when I kill the running program within the batch that program is aborted however, the second program in the batch starts running. I want to completely stop all the programs, ones running as well as the next ones in line.
I submit the batch using Task Scheduler (start at a specific time) however, when I use the Task Manager same thing happens. stops the current one but the program after it kicks in.
Hi, yes that's correct. One single batch containing multiple SAS programs and once the first one finishes then second one runs and goes until the last one completed.
That might be an option, but I guess I have to put a statement at the end of each program to tell the next program that it completed successfully.
@atesera wrote:
Thanks @Quentin for your response. The problem is when I kill the running program within the batch that program is aborted however, the second program in the batch starts running. I want to completely stop all the programs, ones running as well as the next ones in line.
I submit the batch using Task Scheduler (start at a specific time) however, when I use the Task Manager same thing happens. stops the current one but the program after it kicks in.
From how I understand you, you've got some master .bat script that calls .sas programs in sequence. If you execute the .sas programs in foreground (=not using START) then killing the master .bat process should give you the outcome you're after.
Alternatively: Add in your master .bat script some error checking so that .sas programs only run if a previous program didn't return an error condition.
Even though I believe just scheduling some master .sas program with %include statement would also work but I don't like the approach because this would execute all included programs in a single SAS session - and if prog1 changes the environment line changing an option then prog2 will also execute with this changed option.
Yes, correct. So, below is the screenshot from a TEST file where I have the Master.bat batch file that contains two separate .sas programs which runs as a whole. Once the first Job1.sas completes, it jumps to the second one and runs it course. My problem is if manually I kill the batch while Job1.sas is running, it automatically starts the Job2.sas which I don't want to. I want to stop the batch before starting the Job2.sas
@atesera wrote:
Yes, correct. So, below is the screenshot from a TEST file where I have the Master.bat batch file that contains two separate .sas programs which runs as a whole. Once the first Job1.sas completes, it jumps to the second one and runs it course. My problem is if manually I kill the batch while Job1.sas is running, it automatically starts the Job2.sas which I don't want to. I want to stop the batch before starting the Job2.sas
@atesera
You could add some error checking to your Master.bat.
I don't have an environment to test it but below some Copilot generated sample code that should work (or at least that's close to something working)
@echo off echo Running prog1... "c:\Program Files\sas.exe" -sysin "c:\users\prgm\prog1.sas" -log "c:\users\prgm\prog1.log" :: Check the exit status of prog1 if %errorlevel% neq 0 ( echo prog1 encountered an error. Stopping execution. goto end ) echo prog1 completed successfully. Running prog2... "c:\Program Files\sas.exe" -sysin "c:\users\prgm\prog2.sas" -log "c:\users\prgm\prog2.log" :: Check the exit status of prog2 if %errorlevel% neq 0 ( echo prog2 encountered an error. Stopping execution. goto end ) echo prog2 completed successfully. Running prog3... "c:\Program Files\sas.exe" -sysin "c:\users\prgm\prog3.sas" -log "c:\users\prgm\prog3.log" :end echo Script execution completed. pause
@atesera Below a tested script for Python programs run in sequence that will stop as soon as one of the progs doesn't return code 0.
@echo off set my_path=C:\****\01_test_scripts set my_prog=prog1.py echo Running %my_prog%... python "%my_path%\%my_prog%" :: Check the exit status of my_prog if %errorlevel% neq 0 (goto :SharedCode) echo %my_prog% completed successfully. set my_prog=prog2_error.py echo Running %my_prog%... python "%my_path%\%my_prog%" :: Check the exit status of my_prog if %errorlevel% neq 0 (goto :SharedCode) echo %my_prog% completed successfully. set my_prog=prog3.py echo Running %my_prog%... python "%my_path%\%my_prog%" :: Check the exit status of my_prog if %errorlevel% neq 0 (goto :SharedCode) echo %my_prog% completed successfully. goto :eof :: Define the shared code as a label :SharedCode echo %my_prog% encountered an error. Stopping execution. goto :eof
How do you start your batch jobs? If you run them via a scheduler, then typically schedulers have a stop job feature.
I start it using the Task Scheduler (start at a specific time) but i have other batches that are either manual start or by rules on Outlook.
they start with a trigger email or sometimes I manually start the batch. I don't use Task Scheduler.
Agree that doing this via the OS (e.g., if you're using Linux, in an 'at' job) is probably the most direct. However, if you can't do that, you could build something into the start and end of each program (for those higher than #1) that looks for some condition, and if it doesn't find it, it aborts.
%macro startstop(beginning=, lib=);
%if &beginning %then %do;
* check for presence of <LIB>.continue ;
proc sql noprint;
select count(*) into :continue trimmed
from dictionary.tables
WHERE libname=upcase("&lib") and lowcase(memname)='continue';
quit;
%if &continue=0 %then %do;
%put ERROR: batch job terminated before running &sysprocessname;
%abort cancel;
%end;
proc datasets lib=&lib memtype=data nolist nodetails;
delete continue;
run; quit;
%end;
%else %do;
data &lib..continue; x=1; run;
%end;
%mend; *startstop();
** THEN, IN YOUR PROGRAM #1 PUT THIS AT THE **END** ;
%startstop(beginning=0, lib=<YOUR LIBREF>);
** and in PROGRAMS #2 and higher, put this at the beginning: ;
%startstop(beginning=1, lib=<YOUR LIBREF>);
** ... and this at the end ;
%startstop(beginning=0, lib=<YOUR LIBREF>);
@quickbluefish wrote:
...no idea why the SAS editor is squishing the last several lines after the macro into one line after I post it - pretty annoying.
I fixed your post.
It's a weird bug. It happens if you edit the code after you have inserted it, and you don't use the code editor. To avoid the problem, before you edit the code, click the "insert SAS code" button so that you edit it in the code editor, instead of the message editor.
See this thread for a better explanation of the work around: https://communities.sas.com/t5/All-Things-Community/Bug-when-editing-existing-post/m-p/448947#M3084
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.