BookmarkSubscribeRSS Feed
emkotnik
Calcite | Level 5

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!

2 REPLIES 2
Reeza
Super User

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!



 

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1210 views
  • 0 likes
  • 3 in conversation