BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
rodelabrishti
Calcite | Level 5

Hi guys, I need some urgent help please. I have two different dataset, and I want to run a bunch of programs based on those datasets. 

 

So let's say I have variable dtype - 

 

%let dtype = riska riskb; /* these are two different dataset with exact same layout, columns, but slightly different values*/ 

 

I have 5 programs that I want to run, but I want to run all the programs for a first, then b. so I want to loop through the variable dtype, and do something like this 

 

%do 1 %to %sysfunc(countw(%dtype.));

 

%inc "first_program.sas";

%inc "second_program.sas";

%inc "third_program.sas";

%inc "fourth_program.sas";

%inc "fifth_program.sas";

 

%end;

 

I did something similar, but it's only running riska, but not running riskb. I appreciate any help.  

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You left out too many details to tell what you did wrong.

 

For just TWO cases I wouldn't bother with the %DO loop.  Just copy the %INCLUDE statements twice.

Make sure that all of the code know to use the macro variable to reference the dataset.

%let dsname=riska;
%include ('one.sas' 'two.sas' 'three.sas');
%let dsname=riskb;
%include ('one.sas' 'two.sas' 'three.sas');

Or instead have the code always reference the same WORK dataset.

data for_analysis;
  set riska;
run;
%include ('one.sas' 'two.sas' 'three.sas');
data for_analysis;
  set riskb;
run;
%include ('one.sas' 'two.sas' 'three.sas');

If you do need a %DO loop then first make sure it is inside a macro.

%macro looper(dslist);
%local i dsname;
%do i=1 %to %sysfunc(countw(&dslist,%str( )));
  %let dsname=%scan(&dslist,&i,%str( ));
%include ......
%end;
%mend looper;
%looper(riska riskb)

Or perhaps just use a data step to write the code to a file and then include that file.

filename code temp;
data _null_;
  length dsname $41 ;
  file code;
  do dsname='riska','riskb';
    put '%let ' dsname= ';'
      / '%include "one.sas";'
      / '%include "two.sas";'
    ;
  end;
run;
%include code / source2;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You left out too many details to tell what you did wrong.

 

For just TWO cases I wouldn't bother with the %DO loop.  Just copy the %INCLUDE statements twice.

Make sure that all of the code know to use the macro variable to reference the dataset.

%let dsname=riska;
%include ('one.sas' 'two.sas' 'three.sas');
%let dsname=riskb;
%include ('one.sas' 'two.sas' 'three.sas');

Or instead have the code always reference the same WORK dataset.

data for_analysis;
  set riska;
run;
%include ('one.sas' 'two.sas' 'three.sas');
data for_analysis;
  set riskb;
run;
%include ('one.sas' 'two.sas' 'three.sas');

If you do need a %DO loop then first make sure it is inside a macro.

%macro looper(dslist);
%local i dsname;
%do i=1 %to %sysfunc(countw(&dslist,%str( )));
  %let dsname=%scan(&dslist,&i,%str( ));
%include ......
%end;
%mend looper;
%looper(riska riskb)

Or perhaps just use a data step to write the code to a file and then include that file.

filename code temp;
data _null_;
  length dsname $41 ;
  file code;
  do dsname='riska','riskb';
    put '%let ' dsname= ';'
      / '%include "one.sas";'
      / '%include "two.sas";'
    ;
  end;
run;
%include code / source2;
rodelabrishti
Calcite | Level 5
Thank you Tom, I'm going with the first solution for now.
%let dsname=riska;
%include ('one.sas' 'two.sas' 'three.sas');
%let dsname=riskb;
%include ('one.sas' 'two.sas' 'three.sas');
But I want to explore the other one too. I'll post here if I have any questions. I really appreciate it.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 1453 views
  • 2 likes
  • 2 in conversation