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

data test;
if compress(&MON1.)= compress(&MON2.)
then I want to run this program "C:\Nitin(C)\SAS & SQL\SAS\SAS_Excel_VBA_Program.sas";
else Put 'NA';
RUN;

/*I tried call execute and %include but both options not working*/

Any help will be highly appreciated. thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Assuming your external program is not only a code snippet for inclusion into a data step but a full program, here some options how to do this. The sample code below is self-contained and fully functional.

/* create sample demo program demo.sas in SAS Work folder */
%let work_path=%sysfunc(pathname(work));
data _null_;
  file "&work_path/demo.sas";
  put 'proc print data=sashelp.class(obs=1);run;';
run;

%let mon1=Feb2020;
%let mon2=Feb2020;

/* option 1 */
data _null_;
  if compress("&MON1.")= compress("&MON2.") then
    do;
      call execute('%include '||"'&work_path/demo.sas' /source2;");
    end;
  else Put 'NA';
  stop;
RUN;

/* option 2 */
%macro doit(m1,m2); 
  %if &m1=&m2 %then
    %do;
      %include "&work_path/demo.sas" /source2;
    %end;
  %else
    %put NA;
%mend;
%doit(&mon1,&mon2);

/* option 3 (requires a recent SAS release) */
%if &mon1=&mon2 %then
  %do;
    %include "&work_path/demo.sas" /source2;
  %end;
%else
  %do;
    %put NA;
  %end;
 

 

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

Assuming your external program is not only a code snippet for inclusion into a data step but a full program, here some options how to do this. The sample code below is self-contained and fully functional.

/* create sample demo program demo.sas in SAS Work folder */
%let work_path=%sysfunc(pathname(work));
data _null_;
  file "&work_path/demo.sas";
  put 'proc print data=sashelp.class(obs=1);run;';
run;

%let mon1=Feb2020;
%let mon2=Feb2020;

/* option 1 */
data _null_;
  if compress("&MON1.")= compress("&MON2.") then
    do;
      call execute('%include '||"'&work_path/demo.sas' /source2;");
    end;
  else Put 'NA';
  stop;
RUN;

/* option 2 */
%macro doit(m1,m2); 
  %if &m1=&m2 %then
    %do;
      %include "&work_path/demo.sas" /source2;
    %end;
  %else
    %put NA;
%mend;
%doit(&mon1,&mon2);

/* option 3 (requires a recent SAS release) */
%if &mon1=&mon2 %then
  %do;
    %include "&work_path/demo.sas" /source2;
  %end;
%else
  %do;
    %put NA;
  %end;
 

 

nitink26
Obsidian | Level 7

Hi Patrick,

A quick query - why we are using / source2?

Thanks,

Nitin

 

Patrick
Opal | Level 21

The source2 option prints the code from an %include statement to the log. That shows you what got executed and also supports debugging if required.

Btw: You probably should mark my answer as solution and not your "thank you". Except for rare cases it's bad style to mark your own stuff as solution.

nitink26
Obsidian | Level 7

Thanks Patrick!

I marked your answer as solution.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1136 views
  • 4 likes
  • 2 in conversation