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:
Group | count | est |
101 | 32 | 5000 |
102 | 170 | 6000 |
103 | 288 | 7000 |
104 | 267 | 8000 |
105 | 321 | 5000 |
106 | 332 | 6000 |
107 | 214 | 7000 |
201 | 33 | 8000 |
202 | 63 | 5000 |
203 | 326 | 6000 |
204 | 202 | 7000 |
205 | 73 | 8000 |
206 | 63 | 5000 |
207 | 215 | 6000 |
208 | 85 | 7000 |
209 | 53 | 8000 |
210 | 97 | 5000 |
211 | 233 | 6000 |
301 | 160 | 7000 |
302 | 148 | 8000 |
303 | 127 | 5000 |
304 | 36 | 6000 |
305 | 15 | 7000 |
306 | 30 | 8000 |
307 | 23 | 5000 |
308 | 9 | 6000 |
309 | 52 | 7000 |
310 | 11 | 8000 |
401 | 65 | 5000 |
402 | 229 | 6000 |
403 | 31 | 7000 |
404 | 7 | 8000 |
405 | 76 | 5000 |
406 | 15 | 6000 |
407 | 114 | 7000 |
408 | 166 | 8000 |
409 | 60 | 5000 |
410 | 97 | 6000 |
501 | 68 | 7000 |
502 | 76 | 8000 |
/*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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.