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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Try this:

 

%let i=%scan(&list,&n,%str( ));
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Try this:

 

%let i=%scan(&list,&n,%str( ));
--
Paige Miller
Tommy1
Quartz | Level 8

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 :). 

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