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

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.

 

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
  • 1221 views
  • 0 likes
  • 3 in conversation