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.
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;
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;
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.
Ready to level-up your skills? Choose your own adventure.