Hi All,
I have a master program which executes multiple sas programs based on different conditions between each other. So here based on a particular confition, I want sas not to process any furthur datasets/statements (SAS CODE OUTSIDE MACRO ALSO) .
please check sample code below and code below should not execute code for dataset Class2. Please suggest me how to achieve this. (I tried with "Return" statement but it still executes 'class2' and if we use GOTO statemnt, we should write lable with in Macro only). Your help is appreciated.
data class;
set sashelp.class;
where sex='n';
run;
%macro chk;
proc sql;
select count(*) into :cnt from class;
quit;
%if &cnt. Le 0 %then %do;
%put 'data is not Available';
%put 'SAS will exit and not process any furthur datasets';
%end;
%else %put 'Data is Available';
%mend;
%chk;
data class2;
set sashelp.class;
run;
Thanks,
Hari.
Hi,
sorry for late reply.
"endsas" works in local pc sas. however , I want to end it remote session and unfortunately its not working when I submit between RSUBMIT and ENDRSUBMIT. I want this to work between rsubmit and endrsubmit, becaue our process runs remotely and any condition that I chekc will be in remote only. so based on condition, SAS sessin should end.
You can do it in a number of ways. Are you using EnterriseGuide for intance as that has conditionals on program execution. You can do it in code in a number of ways also:
data class; set sashelp.class; where sex='n'; run; data _null_; set sashelp.vtable (where=(libname="WORK" and memname="CLASS")); if nobs=0 then call execute('%put "No obs found, nothing further done";'); else call execute('%put "Obs found, doing something";'); run;
Using code generation. You could also call macros or other code in those blocks, or include other programs
Hi,
Thanks.
I don't use EG rather I use PC SAS and will submit in remote session.
could you please use code which I have posted, run and make it not to execute the code which is written for Classe 2 datset creation.
I should stop SAS not to process any thing furthur at all when Number o frecords in class is <= 0 (As per code which I posted earlier).
Thank,
Hari.
Try %ABORT;
data class;
set sashelp.class;
where sex='n';
run;
%macro chk;
proc sql;
select count(*) into :cnt from class;
quit;
%if &cnt. Le 0 %then %do;
%put 'data is not Available';
%put 'SAS will exit and not process any furthur datasets';
%end;
%else %put 'Data is Available';
%abort;
%mend;
%chk
data class2;
set sashelp.class;
run;
Hi,
Even %Abort will work same as %return. In our case, the given code still creates Class2 dataset. pleaes gove any other alternative.
If you want to run last step when there are more than 0 observations then
enter your conditional code to run inside the macro:
%macro chk;
proc sql;
select count(*) into :cnt from class;
quit;
%if &cnt. Le 0 %then %do;
%put 'data is not Available';
%put 'SAS will exit and not process any furthur datasets';
%end;
%else %do;
%put 'Data is Available';
data class2;
set sashelp.class;
run;
%end;
%mend;
%chk;
Thanks you Sir.
but this is not what I am expecting. In my actual code, I will have many other data steps outside of Macro and I should not execute any of them when a certain conditon with in that macro is satisfied. please check again and revert.
Then save the conditional code to run as a separate file and do:
%macro chk;
proc sql;
select count(*) into :cnt from class;
quit;
%if &cnt. Le 0 %then %do;
%put 'data is not Available';
%put 'SAS will exit and not process any furthur datasets';
%end;
%else %do;
%put 'Data is Available';
%include '...path_and_name_of_code_to_run.sas';
%end;
%mend;
%chk;
Use
ENDSAS;
instead of
%abort;
?
Hi,
sorry for late reply.
"endsas" works in local pc sas. however , I want to end it remote session and unfortunately its not working when I submit between RSUBMIT and ENDRSUBMIT. I want this to work between rsubmit and endrsubmit, becaue our process runs remotely and any condition that I chekc will be in remote only. so based on condition, SAS sessin should end.
Hvae you tried to use the macro program I have posted ?
The logic of it is:
rsubmit;
%macro macro_name;
%if <condition> is true
%then %do;
<run code-1> ;
%end;
%else do;
<run code-2>;
%end;
%mend macro_name;
%macro_name;
endrsubmit;
where <run_code> is any sas code or program included.
Hi Sir,
I tried this too. It's a direct code or through #include, the challenge is sas was executing rest of the code after macro. I dont want that to happen because once I find any error in any step sas should not execute any statements furthur.
Now finally some how I am able to do it using endsas itself.
Thanks for your suggetions.
Hari,
I would suggest you look at the following article to see if it can play into your scenario (vs just the mockup code below):
This is the direction I would look at facing similar issues in the past. YMMV.
Add this to your %THEN %DO loop to see if it provides the desired result:
data _null_; abort cancel; run;
Vince DelGobbo
SAS R&D
Hi, This works but the challenge is sas will still execute statements outside macro/step which are written after. This should not happen.
endsas option is working for me. thanks for your suggetion.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.