Desktop productivity for business analysts and programmers

Error while using SAS if in the main body of the program

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

Error while using SAS if in the main body of the program

I want my SAS program to exit if a certain error condition is met.

So in the program outside any proc, data, macro declarations, I write:

if (&errCount > 1) then abort;

I get:

ERROR 180-322: Statement is not valid or it is used out of proper order.

How to solve it?


Accepted Solutions
Solution
‎05-12-2014 08:10 PM
Trusted Advisor
Posts: 1,052

Re: Error while using SAS if in the main body of the program

I had taken from the "I want my SAS program to exit if a certain error condition is met." that this is exactly what the OP wants. However, it's worth keeping in mind that WORK datasets won't be available for further examination.

Tom

View solution in original post


All Replies
Respected Advisor
Posts: 3,063

Re: Error while using SAS if in the main body of the program

The ABORT statement is only available in a DATA step:

data _null_;

  if (&errCount > 1) then abort;

run;

http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#p0hp2evpgqvfsfn...

Alternatively you could use %ABORT but that would need conditional macro logic around it inside a macro:

%macro check_error;

%if  (&errCount > 1) %then %abort;

%mend;

Frequent Contributor
Posts: 82

Re: Error while using SAS if in the main body of the program

Thanks. I tried the first option.

data _null_;

  if (&errCount > 1) then abort;

run;

I got this in log:

ERROR: Execution terminated by an ABORT statement at line 34 column 31.

Which is ok, but then the rest of the program went on executing as if nothing had happened.

Trusted Advisor
Posts: 1,052

Re: Error while using SAS if in the main body of the program

By gar, you're right!

ABEND has different behaviours in batch and interactive. To get it to stop executing in interactive mode, use ABORT ABEND.

Tom

Community Manager
Posts: 2,693

Re: Error while using SAS if in the main body of the program

It's not a good idea to use ABORT ABEND within EG, as it will close your connection with the SAS Workspace. You'll lose your connection to work data, output, and any remaining log messages.  It's a bit like hanging up the phone in the middle of a conversation.

Perfectly fine to use in a batch job though!

Chris

Solution
‎05-12-2014 08:10 PM
Trusted Advisor
Posts: 1,052

Re: Error while using SAS if in the main body of the program

I had taken from the "I want my SAS program to exit if a certain error condition is met." that this is exactly what the OP wants. However, it's worth keeping in mind that WORK datasets won't be available for further examination.

Tom

Respected Advisor
Posts: 3,063

Re: Error while using SAS if in the main body of the program

As an interesting aside I have a SAS program that contains an ABORT ABEND. When run in EG I dont want it to abort - no good hanging up mid-conversation as Chris says!

But when run in batch I do want it to abort. Hence I check the _CLIENTAPP macro variable and if populated assume I'm in EG, if not I am in batch. If anyone knows a better way of checking where the program is running I'd be interested. The old SYSENV option is no longer useful as it has the same value for EG and batch.

Respected Advisor
Posts: 4,969

Re: Error while using SAS if in the main body of the program

I find that I'm always using %sysfunc(getoption(sysin)) if only to add it to the title.  It returns the complete path to the program in batch, but returns a dot in any form of interactive use.  As long as I am already retrieving it, that's how I distinguish batch use from interactive.

Valued Guide
Posts: 3,206

Re: Error while using SAS if in the main body of the program

This is structural programming like: Nassi–Shneiderman diagram - Wikipedia, the free encyclopedia

Some rules:

- Avoid "go to's  as leading to spaghetti coding. 

- Error signalling is allowed but nice clean-ups should be done

How to do this in SAS? Look what DI is generating a lot of steps just for checking processes and doing error recognition.
The abort should work but I remember there was a situation SAS failed in a nice clean-up (within macro and SAS/AF) causing a cpu-loop. 

There is not issue with all this when you do your code in block's and every block does a check on the error-settings before getting executed.

With SAS you only are able to solve this by using macro-s. Thinking in this kind of logical blocks reuse, restart are more easy to implement   

---->-- ja karman --<-----
Frequent Contributor
Posts: 144

Re: Error while using SAS if in the main body of the program

Hello,

If your program is a macro, have you tried with %return? This instrucction stop the current macro. I use this instrucction a lot to control bad usage in macros with a command like that

%if condition %then %return;

Regards

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 673 views
  • 3 likes
  • 7 in conversation