Hi,
I am trying to execute multiple programs conditionally in data step.
eg:
%let mth1=10;
data _null_;
if &mth1 in (01,02,03,04,05,06,07) then do;
if &mth1=01 then;
%include "C:\Users\Desktop\test_mr1.sas";
else;
%include "C:\Users\Desktop\test_mr2.sas";
end;
else do;
if &mth1 in (08,09,10,11,12) then
%include "C:\Users\Desktop\test_mr3.sas";
end;
run;
There is no errors in the log but the programs are not executing.
Please advise.
"/source2" here is part of the %include statement. It makes sure that the included code gets also written to the SAS log as this make debugging much easier.
A few errors:
1. %include doens't operate conditionally, it will execute all.
2. If THEN isn't correct SAS syntax, it's
IF <condition> Then DO;
<code>
END;
Since you're only using a single statement you don't the DO/END block.
3. If you're comparing to a string, you need quotes.
I think you can try call execute instead, but I haven't tested it.
if "&mth" in ("01", "02", "03" .. "07") then do;
if "&mth1"="01" then call execute('%include "C:\Users\Desktop\test_mr1.sas";');
else call execute(...);
end;
else if "&mth" in ("08", "09", .. "12") then call execute(...);
else put "Not a valid month";
Thanks Reeza!
I made the changes as per you suggestion but still the code is not producing any results.
Here you go:
Code:
%let mth1=10;
data _null_;
if "&mth1" in ("08","09","10","11","12") then
call execute('C:\Users\Desktop\test_mr.sas;');
run;
Log:
130 data _null_;
131
SYMBOLGEN: Macro variable MTH1 resolves to 10
132 if "&mth1" in ("08","09","10","11","12") then
133 call execute('C:\Users\Desktop\test_mr.sas;');
134 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
Yeah, you dropped the %include. It was in my code, but not in yours.
You need a macro program to do it:
/*** Define the macro program ***/
%macro run_prog(mm);
%if %eval(&mm) ge 01 and %eval(&mm) le 07 %then %do;
%if &mm = 01 %then %do;
%include "C:\Users\Desktop\test_mr1.sas";
%end;
%else %do;
%include "C:\Users\Desktop\test_mr2.sas";
%end;
%end; %else
%if %eval(&mm) ge 08 and %eval(&mm) le 12 %then %do;
%include "C:\Users\Desktop\test_mr3.sas";
%end;
%mend run_prog;
/*** run the macro with desired argument ***/
%run_prog(10);
Thanks Reeza!
Just curious why a macro program was need instead of Call Execute?
Maybe, if you use
call execute('%include "C:\Users\Desktop\test_mr.sas;" ');
instead
call execute('C:\Users\Desktop\test_mr.sas;');
it will work too. It worth a test;
As suggested by @Reeza
data _null_;
file 'c:\temp\test_mr1.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr1.sas"; stop; run;';
file 'c:\temp\test_mr2.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr2.sas"; stop; run;';
file 'c:\temp\test_mr3.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr3.sas"; stop; run;';
stop;
run;
%let mth1=10;
data _null_;
if &mth1 in (01,02,03,04,05,06,07) then
do;
if &mth1=01 then
call execute('%include "c:\temp\test_mr1.sas" /source2;');
else call execute('%include "c:\temp\test_mr2.sas" /source2;');
end;
else if &mth1 in (08,09,10,11,12) then
call execute('%include "c:\temp\test_mr3.sas" /source2;');
run;
SAS Forum: Conditionally executing multiple programs in data step
It is my belief that DOSUBL is prefered over other methods
like 'call execute' and macros because two way communication
is possible and the solution resides in one address space
which should have a performance advantage?
HAVE three programs, I would like to conditionally execute based on the month (example)
if month=1 then %include "c:\temp\test_mr1.sas" /source2
if month=2 then %include "c:\temp\test_mr2.sas" /source2
if month=3 then %include "c:\temp\test_mr3.sas" /source2
WANT this text in the log
month=1 c:\temp\test_mr1.sas was executed
inpired by
https://goo.gl/jdwAjW
https://communities.sas.com/t5/Base-SAS-Programming/Conditionally-executing-multiple-programs-in-data-step/m-p/304816
SOLUTION working code
dosub:
rc=dosubl(resolve('
%include &prg.;
'));
return;
FULL SOLUTION
data _null_;
file 'c:\temp\test_mr1.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr1.sas"; stop; run;';
file 'c:\temp\test_mr2.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr2.sas"; stop; run;';
file 'c:\temp\test_mr3.sas';
put
'data _null_; put "program executed is: c:\temp\test_mr3.sas"; stop; run;';
stop;
run;
%let mth1=10;
data _null_;
if &mth1 in (01,02,03,04,05,06,07) then
do;
if &mth1=01 then do;
call symputx('prg','"c:\temp\test_mr1.sas" /source2');
goto dosub;
end;
else do;
call symputx('prg','"c:\temp\test_mr2.sas" /source2');
goto dosub;
end;
end;
else if &mth1 in (08,09,10,11,12) then do;
call symputx('prg','"c:\temp\test_mr2.sas" /source2');
goto dosub;
end;
return;
dosub:
rc=dosubl(resolve('
%put &=mth1;
%include &prg.;
'));
return;
run;
Hi Patrick,
Thanks for your feedback.
I am just curious to know why you incluced
/source2
in the call excecute. What does /source2 means?
Please let me know.
Thank you!
"/source2" here is part of the %include statement. It makes sure that the included code gets also written to the SAS log as this make debugging much easier.
Thank you!
An alternative way is using SAS/IML's CALL EXECUTEFILE() .
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!
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.