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?
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
The ABORT statement is only available in a DATA step:
data _null_;
if (&errCount > 1) then abort;
run;
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;
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.
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
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
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
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.
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.
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
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.