Desktop productivity for business analysts and programmers

How do end SAS EG when a condition is met?

Reply
Contributor
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;

run;

But it's not good!

Any ideal?

Thanks in advance!

Contributor
Posts: 45

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 (http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000230210.htm) 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.

Tim.

/* AutoExec code */

%global Cancel SQLCancel;

%macro Reset_Errors();

     %let Cancel=;

     %let SQLCancel=;

%mend;

%macro Raise_Error(Text);

     %let Cancel=cancel;

     %let SQLCancel=noexec;

     %put ERROR-My Error: &text;

%mend;

/* Program */

%Reset_Errors;

data m02;

     length myVar 8;

     input myVar;

     datalines;

1

2

3

4

;

run &Cancel;

%let MyVar=0;

data Test1;

     set m02;

     if myVar=3 then do;

          call symputx('MyVar',1);

          stop;

     end;

run &Cancel;

%macro Check_MyVar();

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

%mend;

%Check_MyVar;

data Test2;

     set m02;

run &Cancel;

proc sql &sqlcancel;

     select * from m02;

quit;

%Reset_Errors;

Community Manager
Posts: 2,711

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

Hi,

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

Chris

Contributor
Posts: 25

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

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  .

Trusted Advisor
Posts: 1,061

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.

Tom

Contributor
Posts: 25

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

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 (7.100.0.2002) (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.

Respected Advisor
Posts: 3,069

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

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
  • 3181 views
  • 0 likes
  • 6 in conversation