I am lookin for a macros code which can repeat the same step based on the variable and create separate output
my example data . I want to create aggregate (sum of population) at different levels of Age_1, Age_5 and Geo for each year. Orginally I am working with 30 Year data, would like to repeat proc means using macros
Year | Age_1 | Age5 | Geo | Population |
2000 | 1 | 1 to 5 | A | 1.28 |
2000 | 3 | 1 to 5 | A | 2.35 |
2000 | 24 | 20-25 | B | 2.66 |
… | ||||
2000 | 60 | C | 1.87 | |
2019 | 1 | 1 to 5 | A | 2.35 |
2019 | 3 | 1 to 5 | A | 2.66 |
2019 | 24 | 20-25 | B | 2.66 |
2019 | … | 1.87 | ||
2019 | 60 | C | 1.87 |
PROC means DATA =test noprint sum;
class AGE_1 AGE_5 gender geo_levels;
var population;
output out=result sum=;
RUN;
Any help or pointers is really appreciated.
Thanks in advance
That's very simply done with PROC MEANS. Sort your dataset by year, then combine BY and CLASS:
pric means data=test noprint sum;
by year;
class AGE_1 AGE_5 gender geo_levels;
var population;
output out=result1 sum=;
run;
Macros are probably not a good choice here. In your existing code, you have a data set named RESULTS which contains the sum for all values of Age_1, all values of Age_5 and all values of geo
If you need to split things up, don't use a macro and run PROC MEANS many times, just split RESULTS as needed. For example:
proc whatever data=results(where=(Age_1='1'));
or
data want;
set results(where=(age_1='1'));
...
run;
Thank you. But I am not looking to split by AGE_1. I want sum of populations at different levels of Age_1,AGE_5, Geo levels for each fiscal year
Show two or three iterations of what proc means would look like if you were not using a macro. That helps to show how you the macro should be designed.
@Maha_Kumaran wrote:
Thank you.
I would like the proc means repeated for every fiscal year
Iteration 1:
PROC means DATA =test noprint sum;
class AGE_1 AGE_5 gender geo_levels;
var population;
Where year ="2000";
output out=result1 sum=;
RUN;
Iteration 2:
PROC means DATA =test noprint sum;
class AGE_1 AGE_5 gender geo_levels;
var population;
Where year ="2001";
output out=result2 sum=;
RUN;
I have 30Y data, I don't want to repeat proc means 30 times.
Hope this gives some clarity to my question.
Add variable YEAR into your PROC MEANS in the CLASS statement.
PROC means DATA =test noprint sum;
class year AGE_1 AGE_5 gender geo_levels;
var population;
output out=result2 sum=;
RUN;
No macro needed.
That's very simply done with PROC MEANS. Sort your dataset by year, then combine BY and CLASS:
pric means data=test noprint sum;
by year;
class AGE_1 AGE_5 gender geo_levels;
var population;
output out=result1 sum=;
run;
Thank you!
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.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.