Programming the statistical procedures from SAS

Summing Months by year

Reply
Occasional Contributor
Posts: 9

Summing Months by year

[ Edited ]

I have a dataset with Year in the rows, and  the months Sep-May as the columns. I am trying to sum the months by year, in this case Sep-May is considered one year. I am unsure of how to go about this as I cannot figure out how to sum all 9 months together for about 200 years. 

Here is what I've been working with though I have been unsuccessful. 

 

proc means data=schoolyear sum maxdec=0;
class year;
var Sep Oct Nov Dec Jan Feb Mar Apr May;
run;

 

Thank you in advance!

Super User
Posts: 23,700

Re: Summing Months by year

[ Edited ]

If you're summing within a single row, then you would use the SUM function.

 

data want;
set schoolyear;

annual_total = sum(sep, oct, nov, ... , may); *if the variables are NOT side by side you have to list them all;

annual_total2 = sum(of sep--may); *if the variables are side by side;

run;


emkotnik wrote:

I have a dataset with Year in the rows, and  the months Sep-May as the columns. I am trying to sum the months by year, in this case Sep-May is considered one year. I am unsure of how to go about this as I cannot figure out how to sum all 9 months together for about 200 years. 

Here is what I've been working with though I have been unsuccessful. 

 

proc means data=schoolyear sum maxdec=0;
class year;
var Sep Oct Nov Dec Jan Feb Mar Apr May;
run;

 

Thank you in advance!



 

Super User
Posts: 6,762

Re: Summing Months by year

[ Edited ]

I would sum up in a slightly different order.  First, begin with the program you have, but put the results into an output data set:

 

proc means data=schoolyear noprint nway;
class year;
var Sep Oct Nov Dec Jan Feb Mar Apr May;

output out=monthly_totals (drop=_type_ _freq_) sum=;
run;

 

This gives you a SAS data set with the same 10 variables you started with, but the month variables all contain the SUM of their original values.  In other words, you have a much smaller data set:  200 observations (one for each year) and the same 10 variables.  If you want to add them up, it's easy:

 

data want;

set monthly_totals;

yearly_total = Sep + Oct + Nov + Dec + Jan + Feb + Mar + Apr + May;

run;

 

The advantage of doing the steps in this order occurs when you start with a lot of observations.  You don't need to add up the 9 monthly values for every observation ... just add their totals that come out of PROC MEANS.

Ask a Question
Discussion stats
  • 2 replies
  • 132 views
  • 0 likes
  • 3 in conversation