DATA Step, Macro, Functions and more

Conditionally executing multiple programs in data step

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Conditionally executing multiple programs in data step

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
Solution
‎10-18-2016 07:12 PM
Respected Advisor
Posts: 4,173

Re: Conditionally executing multiple programs in data step

"/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.

View solution in original post


All Replies
Super User
Posts: 19,869

Re: Conditionally executing multiple programs in data step

 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";

 

 

 

Frequent Contributor
Posts: 122

Re: Conditionally executing multiple programs in data step

Thanks Reeza!

 

I made the changes as per you suggestion but still the code is not producing any results.

 

 

Super User
Posts: 19,869

Re: Conditionally executing multiple programs in data step

Post your current code and log.
Frequent Contributor
Posts: 122

Re: Conditionally executing multiple programs in data step

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

 

Super User
Posts: 19,869

Re: Conditionally executing multiple programs in data step

Yeah, you dropped the %include. It was in my code, but not in yours. 

Trusted Advisor
Posts: 1,584

Re: Conditionally executing multiple programs in data step

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);

 

            

 

 

Frequent Contributor
Posts: 122

Re: Conditionally executing multiple programs in data step

Thanks Reeza!

 

Just curious why a macro program was need instead of Call Execute?

 

 

Trusted Advisor
Posts: 1,584

Re: Conditionally executing multiple programs in data step

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;

Respected Advisor
Posts: 4,173

Re: Conditionally executing multiple programs in data step

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;
Valued Guide
Posts: 505

Re: Conditionally executing multiple programs in data step

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;
Frequent Contributor
Posts: 122

Re: Conditionally executing multiple programs in data step

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! 

Solution
‎10-18-2016 07:12 PM
Respected Advisor
Posts: 4,173

Re: Conditionally executing multiple programs in data step

"/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.

Frequent Contributor
Posts: 122

Re: Conditionally executing multiple programs in data step

Thank you!

Super User
Posts: 10,044

Re: Conditionally executing multiple programs in data step

An alternative way is using SAS/IML's CALL EXECUTEFILE() .

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 588 views
  • 0 likes
  • 6 in conversation