I'm struggling to understand how to use correctly the abort statement.
The context is the following: I have a job and prompt combo which I run from SAS Studio. It works fine except the fact that the sas program starts executing again when you go to the sas code tab again, the preview sheet.
I want to avoid this by using a abort statement based on the existence of the macro variables that come from the user prompts.
In the code I'd like to delete this macro variables and put the abort condition at the start of the sas code. I don't attach the real code but the code where I try to approach this funcionality.
What I want to achieve is that the code stops restarting if the macro variables (fed by the prompt) do not exist. To guarantee this I want to delete them at the end of the code.
%let test=arne;
%let test2=nadine;
%put &test &test2;
%SYMDEL test;
%put &test &test2;
data _null_;
if %symexist(test2) then alarm="exists";
put alarm=;
run;
%macro test;
data _null_;
if %symexist(test) then %abort cancel;
put alarm=;
run;
proc freq data=sashelp.cars;
table make;
run;
%mend;
%test;
Hello,
Any chance you can use the below example?
Scroll down on this page :
SAS 9.4 / Viya 3.5
Macro Language Reference
%GOTO Macro Statement
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0jfeh75p72icen1ddd9una5zbmm.htm
to :
Example: Providing Exits in a Large Macro
Good luck,
Koen
Hi Koen,
the %goto statement works in my example, now I have the challenge to wrap all the code into a macro block to be able to use it.
%let test=arne;
%let test2=nadine;
%put &test &test2;
%SYMDEL test;
%put &test &test2;
data _null_;
if %symexist(test2) then alarm="exists";
put alarm=;
run;
%macro help;
data _null_;
%if %symexist(test) %then %goto exit;
put alarm=;
run;
proc freq data=sashelp.cars;
table make;
run;
%exit: %mend;
%help;
OK. Nice!
Let the communities know if you encounter any obstacles in wrapping everything in a macro.
Maybe some of us are able to help you out in that case.
Koen
Thank you, I'm getting closer, but the endsas is a too restrictive option for me. It ends the session.
I'm looking for the option that ends the sas code when entering the call execute.
%let test=arne;
%let test2=nadine;
%put &test &test2;
%SYMDEL test;
%put &test &test2;
data _null_;
if 1=1 then call execute('endsas;');
stop;
run;
data _null_;
if %symexist(test2) then alarm="exists";
put alarm=;
run;
%macro help;
data _null_;
%if not %symexist(test) %then %goto exit;
put alarm=;
run;
proc freq data=sashelp.cars;
table make;
run;
%exit: %mend;
%help;
proc freq data=sashelp.cars;
table make;
run;
Hello,
when you
%goto exit;
, you end the SAS code, no?
If you do not do anything after the macro call, your code stops running (as there's nothing left to run).
No need for "endsas;" statement / command.
Koen
@sbxkoenk The proc freq still runs.
This has been a request ever since I started using SAS, which is well into the last century. See https://communities.sas.com/t5/SASware-Ballot-Ideas/Create-new-statement-STOPROGRAMRIGHTHERE-or-simi...
Within the SASjs framework we constantly have a need for conditional aborts. So, we wrote a macro for it: https://core.sasjs.io/mp__abort_8sas.html
Example usage:
%mp_abort(iftrue=(&somecondition=1)
,mac=&sysmacroname
,msg=%str(Some Condition Failed)
)
This still kills the SAS session right?
Very nice macro library, congrats! 🙂
It kills the session in all cases except for Stored Process sessions - as we found that this could leave orphan multibridge sessions and kill the server for some SAS 9 environments.
In this case, we take a unique approach - we open a macro but don't close it! This means the rest of the code does not get executed (and the STP session ends "normally")
Something like this:
filename skip temp;
data _null_;
file skip;
put '%macro skip();';
comment '%mend skip; -> fix lint ';
put '%macro skippy();';
comment '%mend skippy; -> fix lint ';
run;
%inc skip;
@AllanBowe wrote:
..., we take a unique approach - we open a macro but don't close it! This means the rest of the code does not get executed (and the STP session ends "normally")
Interesting. I have never used that "trick". Something to remember. 🤔
Koen
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.