BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

Hi everyone,

 

I have data that I need to subset 40 times. Instead of running the same 30 lines of code 40 times, I imagine this is where a macro would be useful. But I'm not quite sure where to start. I need the macro to subset the groups sequentally. 

 

This is the code that I would like to repeat 40 times and some sample data: 

 

Groupcountest
101325000
1021706000
1032887000
1042678000
1053215000
1063326000
1072147000
201338000
202635000
2033266000
2042027000
205738000
206635000
2072156000
208857000
209538000
210975000
2112336000
3011607000
3021488000
3031275000
304366000
305157000
306308000
307235000
30896000
309527000
310118000
401655000
4022296000
403317000
40478000
405765000
406156000
4071147000
4081668000
409605000
410976000
501687000
502768000

 

/*Subset group 101*/
 data Sumnum101; set sumnum;
 if group=101;
run;

 data Sumpop101; set sumpop;
 if group=101;
run;

proc sort data=Sumnum101; by age5; run;
proc sort data=Sumpop101; by age5; run;

data numpop101; 
merge Sumnum101 Sumpop101; by age5; 
if age5=. then delete;
crate=(count/est)*100000;
if age5=1 then weight=0.25773;
         else if age5=2 then weight=0.09561;
         else if age5=3 then weight=0.29819;
         else if age5=4 then weight=0.22208;
         else if age5=5 then weight=0.12639;
/*Adjusted Rate*/
arate = crate*weight; 
run; 


proc means data = numpop101; 
 var arate; 
 output out=ADJRATE sum=; run;

Essentally, I'd like the group variable to be the suffix to each dataset name and the grouping/subset variable.

1 REPLY 1
Reeza
Super User

Don't. Add another BY group to your processing instead. 

What happens with the folllowing? You have shown one data set, but reference two data sets.

 



proc sort data=Sumnum101; by group age5; run;
proc sort data=Sumpop101; by group age5; run;

data numpop101; 
merge Sumnum101 Sumpop101; by group age5; 
if age5=. then delete;
crate=(count/est)*100000;
if age5=1 then weight=0.25773;
         else if age5=2 then weight=0.09561;
         else if age5=3 then weight=0.29819;
         else if age5=4 then weight=0.22208;
         else if age5=5 then weight=0.12639;
/*Adjusted Rate*/
arate = crate*weight; 
run; 


proc means data = numpop away; 
class group;
 var arate; 
 output out=ADJRATE sum=; 
run;

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1 reply
  • 887 views
  • 0 likes
  • 2 in conversation