DATA Step, Macro, Functions and more

SAS Macro Repeat Programs

Reply
Contributor
Posts: 50

SAS Macro Repeat Programs

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.

Super User
Posts: 17,819

Re: SAS Macro Repeat Programs

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;

 

Ask a Question
Discussion stats
  • 1 reply
  • 132 views
  • 0 likes
  • 2 in conversation