Desktop productivity for business analysts and programmers

How do end SAS EG when a condition is met?

Posts: 25

How do end SAS EG when a condition is met?

Hi everybody,

How do end SAS EG when a condition is met?

Here is my code:

option errorabend;

data _NULL_;

     set m02(keep=flag);

     if flag="Y" then endsas;


But it's not good!

Any ideal?

Thanks in advance!

Posts: 50

Re: How do end SAS EG when a condition is met?

Hi summy,

I think this depends on the state you want EG to be left in when you stop the process...

The 'proper' way of doing this I would guess would be to use STOP or ABORT in your data step. I have never managed to get this working as I would like but here is what I found,

If you use ABORT RETURN then the program will stop running however it will also stop your EG session meaning you will need to reconnect your connection profile before you can continue and you will lose any output that has already been run elsewhere in the project.

Looking at this page ( it looks like you can put options in to stop this but i have never got it working.

Alternatively, the way I handle this is by creating my own error handling. The code below has the extra macros I use at the start but I normally have these in an autoexec flow for my project so I can use them everywhere. For this to work you need to put &Cancel after every run statement and &SQLCancel in your proc SQL statements.

I tend to go overkill and put a %Reset_Errors at the start and end of every program but if you want a raised error to stop other programs from running too you could leave it out or have different levels of raised errors.

Hope it helps.


/* AutoExec code */

%global Cancel SQLCancel;

%macro Reset_Errors();

     %let Cancel=;

     %let SQLCancel=;


%macro Raise_Error(Text);

     %let Cancel=cancel;

     %let SQLCancel=noexec;

     %put ERROR-My Error: &text;


/* Program */


data m02;

     length myVar 8;

     input myVar;







run &Cancel;

%let MyVar=0;

data Test1;

     set m02;

     if myVar=3 then do;

          call symputx('MyVar',1);



run &Cancel;

%macro Check_MyVar();

     %if &MyVar=1 %then %Raise_Error(This is an error);



data Test2;

     set m02;

run &Cancel;

proc sql &sqlcancel;

     select * from m02;



Community Manager
Posts: 3,449

Re: How do end SAS EG when a condition is met?

Posted in reply to TimCampbell


ERRORABEND and ENDSAS are generally not good to use within EG, as it ends the SAS Workspace session immediately and prevents EG from collecting any remaining results, including SAS log and ODS output.  It's the equivalent of "hanging up the phone" before you've had a chance to hear the bad news from your SAS program.

The same holds true for other uses of SAS Workspaces, such as SAS stored processes.  It's better to write your program to detect the bad conditions and then conditionally run program segments only when all good conditions are met. 

You can use Conditions in your EG process flow to run a set of tasks or programs only when the condition is met.  Conditions can be based on prompt values, macro variables, and data set values.  See the SAS Enterprise Guide online help for more about setting Conditions.

Here's an old post with a brief description of Conditions:

SAS programmers are welcome here! - The SAS Dummy


Posts: 25

Re: How do end SAS EG when a condition is met?

Posted in reply to ChrisHemedinger

Actually,I want to first determine whether DWH is avaiable or not ,if not,then

I have to stop the Program Jobs which produce results and transfer

files to other colleague,cause the Project need to be run automatically.That is,end SAS EG,and restart EG project when DWH are avaiable.
I am used to SAS programmer,and along with SAS EG four months.
Just learn from your Blog,tomorrow I'll check which version of My SAS

EG.I'm afraid that it is version 4.1  .

Posts: 1,333

Re: How do end SAS EG when a condition is met?

You also might want to look into SAS DIS. It is a product that is better designed for running software in a "production" type of environment, whereas I consider EG better for development, testing, and adhoc analyses, precisely because this type of issue comes up.


Posts: 25

Re: How do end SAS EG when a condition is met?

Posted in reply to TimCampbell

Thanks,Tim.Your method is very instructive.

New Contributor
Posts: 4

Re: How do end SAS EG when a condition is met?

I'm running SAS 9.4 M3 from Enterprise Guide 7.1 HF1 ( (32-bit).  We have a grid environment with 4 nodes and have grid launched workspace servers enabled (so everything runs on the grid from a local submit).


I have a process that has 10 sub processes that can run in parallel.  Some take round 30 minutes and some take a few seconds.  To allow the parallel execution I turned on the option under project properties / code submission to "Allow Parallel Execution on the same Server" and it runs for the most part how I'd expect.  All 10 processes run at the same time.


Two things I've been struggling with however and was wondering if there is a better way or if not maybe these could be enhancements.  1.  It would be nice if the program coud set the max number of jobs to run at once.  I have found this number interms of what is ideal for 2 to 10 depending on how many cores you have available.  So if you could set this variable within E.G. either in the project properties or in the code itself that would be a big help


2.  I would like a graceful way to ENDSAS with some code.  I don't think SIGNOFF is going to work because these grid launched workspace servers don't require rsubmits.  When I run ENDSAS it seems to work for the most part but I get EG errors.  I still get the log results and everything runs as expected so really if there was a way to suppress those E.G. errors when I run ENDSAS it maybe the best solution.


I really like this solution to grid enable jobs vs wrapping in sca code because of it's simplicity.

Super User
Posts: 4,013

Re: How do end SAS EG when a condition is met?

Posted in reply to benfirst321

If you want better visibility for your post it would be better to open a new thread and reference the old one with a link, especially since your topic is a lot broader.

Ask a Question
Discussion stats
  • 7 replies
  • 6 in conversation