BookmarkSubscribeRSS Feed
Reeza
Super User

Did the code provided work? If there’s an issue I’ll take another look but it should work. If it doesn’t include your code, log and a description of the issue. 

Errant
Obsidian | Level 7

I actually had to switch gears for a couple hours, but I'm going get back to that specific problem. I'll definitely keep you updated, thank you!

PaigeMiller
Diamond | Level 26

@Errant wrote:

So I've reviewed the original requirements and I misinterpreted the requirements. Ultimately I do need to combine the month output with the yearly average output, but the month output actually needs to look like this:

 

ideal.PNG

So technically I'm not calculating an average for the month output. I'm confused about how to get this from the original dataset though.

 


Part of the problem we are having with your words is that you keep talking about a "yearly average" even though the table displayed does not contain yearly averages, it contains monthly averages (as far as I can tell). Where are the yearly averages in this table?

 

Then you say "technically I'm not calculating an average for the month" but yet it seems that where you have typed "price" is indeed an average for the month. If it is not a monthly average, the what is "price"?

 

@Errant, in my opinion, the problem we are having here is not SAS. The table you show is a simple PROC REPORT.  The problem which is preventing us from making progress is that your description of the problem has changed multiple times and doesn't seem to match the latest table you are showing us, as far as I can tell.

 

 

--
Paige Miller
s_lassen
Meteorite | Level 14

Not quite sure if this is what you want to do, but if you want the rolling average of the last 12 observations, it can be done something like this:

data want;
  set qfour.aveprices;
  array a_gas (0:11) 8 _temporary_;
array a_milk (0:11) 8 _temporary_;
array a_eggs (0:11) 8 _temporary_;
_N_=mod(_N_,12); /* array index */
a_gas(_N_)=gas;
a_milk(_N_)=milk;
a_eggs(_N_)=eggs;
gas_mean=mean(of a_gas(*));
milk_mean=mean(of a_milk(*));
eggs_mean=mean(of a_eggs(*));
run;

 Note the use of zero-based arrays (the 0:11construct). They make the code simpler (array index is simply mod(_N_,12)) and execute slightly faster (simpler calculation of address offset).

Patrick
Opal | Level 21

@s_lassen

For the special case here where we've got already the month variable AND assuming there are no missing months, below could work as well.

data have;
  length Year Month 8 Commodity $4 Price 8;
  do commodity='Gas ', 'Milk', 'Eggs';
    do year=2004 to 2014;
      do month=1 to 12;
        price+2;
        output;
      end;
    end;
  end;
  stop;
run;

proc sort data=have;
  by Commodity year month;
run;

data want;
  set have;
  by Commodity year month;
  array _price {12} _temporary_;
  if first.commodity then
    do;
      call missing(of _price[*]);
    end;
  _price[month]=price;
  price_avg=mean(of _price{*});
run;
PaigeMiller
Diamond | Level 26

Now that we've seen the data, @Reeza has come up with the simplest and most straight forward solution, use PROC MEANS/PROC SUMMARY.

 

I guess I don't understand why people are using a data step to solve this problem of finding an average of these data.

 

To @Errant, who said:

I think I need to create a dataset for each product and then perhaps I can create a variable that calculates the average of every 12 obs. 

No you don't need any of this. One dataset will work, then run PROC MEANS/PROC SUMMARY on the data, shown in the reply above by Reeza.

--
Paige Miller

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 20 replies
  • 3390 views
  • 4 likes
  • 6 in conversation