BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
renjithr
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

15 REPLIES 15
Reeza
Super User

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

 

 

 

renjithr
Quartz | Level 8

Thanks Reeza!

 

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

 

 

Reeza
Super User
Post your current code and log.
renjithr
Quartz | Level 8

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

 

Reeza
Super User

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

Shmuel
Garnet | Level 18

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

 

            

 

 

renjithr
Quartz | Level 8

Thanks Reeza!

 

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

 

 

Shmuel
Garnet | Level 18

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;

Patrick
Opal | Level 21

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;
rogerjdeangelis
Barite | Level 11
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;
renjithr
Quartz | Level 8

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! 

Patrick
Opal | Level 21

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

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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