- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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";
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Reeza!
I made the changes as per you suggestion but still the code is not producing any results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yeah, you dropped the %include. It was in my code, but not in yours.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Reeza!
Just curious why a macro program was need instead of Call Execute?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
An alternative way is using SAS/IML's CALL EXECUTEFILE() .