DATA Step, Macro, Functions and more

How to end sas session conditionally in Batch mode (UNIX)

Reply
Contributor
Posts: 40

How to end sas session conditionally in Batch mode (UNIX)

Hi

My SAS code has mutliple data step and proc steps. I want end the sas session when certain condition met.

I am running my sas code on UNIX in batch mode. When certain dataset has 0 observations, I want to end session. I used following is example code, it's not working as I expected.

..multiple sas data, proc steps

data _null_;

set old_dataset nobs=_nobs;

if _nobs=0 then call system('endsas');

run;

..multiple data steps.

Thank you

Super User
Super User
Posts: 7,076

Re: How to end sas session conditionally in Batch mode (UNIX)

You need to test NOBS before the SET statement executes. Otherwise the data step stops as soon as the SET statement tries to read past the end of the file.

data _null_;

if _nobs=0 then call system('endsas');

else stop;

set old_dataset nobs=_nobs;

run;

Contributor
Posts: 40

Re: How to end sas session conditionally in Batch mode (UNIX)

Than you for reply. I modified the code and ran the batch job. The sas session not terminating, still continue to process further sas steps. am I missing to add anything? endsas is not terminating session here.

Super User
Super User
Posts: 7,076

Re: How to end sas session conditionally in Batch mode (UNIX)

Did you remember to include the semi-colon for the ENDSAS statement in the CALL EXECUTE() call?

Trusted Advisor
Posts: 3,215

Re: How to end sas session conditionally in Batch mode (UNIX)

Statements endsas and errorabend are doable in batch, developping in Eguide (and others) it is a bad approach.  https://communities.sas.com/message/148347 see CH
In structures programming it is better to validate all conditions at the first steps. Than only whan all input is valid to proceed

With a bathc approach don't make the program too big. smaller pieces are easily connected. Restarting / recovering will become more easy.

---->-- ja karman --<-----
Super User
Posts: 7,859

Re: How to end sas session conditionally in Batch mode (UNIX)

call system runs a system process, in this case it will try to run the program endsas if such is found in your search path.

I guess you want to use call execute, but why go to such a hassle?

if _nobs = 0 then abort abend 4;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 5 replies
  • 462 views
  • 1 like
  • 4 in conversation