Hi SAS Community, I not very good at using macros yet.
I have a process that I run already (See below), but I have to change the model macro variable and rerun it each time I run it.
%let model= a /*b c d e*/;
DATA a&model;
SET &model(Keep=&model);
RUN;
DATA a2&model;
SET &Data2(Keep=&model);
run;
proc freq data=a&model noprint ;
tables &model/ out=base&model;
run;
proc freq data=a2&model noprint;
tables &model/ out=current&model;
run;
data finbase&model; set base&model;
Bin=&model;
Counts_Training= COUNT;
Percent_Total_Training= PERCENT;
drop &model count percent;
run;
/*... (skipped a few steps just to make display shorter)*/
proc print data=final2&i noobs label;
title1 "PSI for &model.: &q &model &year ";
sum counts_training percent_total_training counts_validation percent_total_validation PSI;
run;
Now I want to take want to take what I have and loop through all the values of the model macro variable.
%let model = a b c d e;
I tried following the example https://communities.sas.com/t5/General-SAS-Programming/Looping-with-Variable-Names/td-p/365111, but I am not doing it correctly because it should finish with a proc print for each iteration and nothing is printing out. What I tried was
%let model= a b c d e
%macro test (list= &model)
%local n i;
%do n=1 %to %sysfunc(countw(&list));
%let i=%scan(&list,&n);
DATA a&i;
SET &i(Keep=&i);
RUN;
DATA a2&i;
SET &Data2(Keep=&i);
run;
proc freq data=a&i noprint ;
tables &i/ out=base&i;
run;
proc freq data=a2&i noprint;
tables &i/ out=current&i;
run;
data finbase&i;
set base&i;
Bin=&i;
Counts_Training= COUNT;
Percent_Total_Training= PERCENT;
drop &i count percent;
run;
data fincur&i;
set current&i;
Bin=&i;
Counts_Validation= COUNT;
Percent_Total_Validation= PERCENT;
drop &i count percent;
run;
/*...*/
proc print data=final2&i noobs label;
title1 "PSI for &i.: &q &i &year ";
sum counts_training percent_total_training counts_validation percent_total_validation PSI;
run;
%end;
%mend;
%test;
If anyone could help me figure what I am doing wrong and how I could loop through this that would be extremely helpful!
(I don't know why the formatting is appearing wierd.)
Try this:
%let i=%scan(&list,&n,%str( ));
Thanks Paige, it works exactly like I need it to! It wasn't working for a while, but I had made a simple SAS mistake and was missing a semi-colon :).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.