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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 630 views
  • 0 likes
  • 2 in conversation