BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Maha_Kumaran
Calcite | Level 5

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_1Age5GeoPopulation
200011 to 5A1.28
200031 to 5A2.35
20002420-25B2.66
    
200060 C1.87
201911 to 5A2.35
201931 to 5A2.66
20192420-25B2.66
2019  1.87
201960 C1.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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Maha_Kumaran
Calcite | Level 5

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

 

Reeza
Super User

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
Calcite | Level 5
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.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Kurt_Bremser
Super User

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;
Maha_Kumaran
Calcite | Level 5

Thank you!

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 626 views
  • 0 likes
  • 4 in conversation