Hye guys,
I included a screenshot to help clarify my problem:
http://i40.tinypic.com/mcrnmv.jpg.
I'm trying to calculate some kind of moving average and moving standard deviation. The thing is I want to calculate the coefficients of variation (stdev/avg) for the actual value. Normally this is done by calculating the stdev and avg for the past 5 years. However sometimes there will be observations in my database for which I do not have the information of the past 5 years (maybe only 3, 2 etc). That's why i want a code that will calculate the avg and stdev even if there is no information for the whole 5 years.
Also, as you see in the observations, sometimes I have information over more than 5 years, when this is the case I need some kind of moving average which allows me to calculate the avg and stdev for the past 5 year. So if a company has information for 7 years I need some kind of code that will calculate the avg and stdev for, lets say, 1997 (by 1991-1996), 1998 (by 1992-1997) and 1999 (1993-1998).
As i'm not very familiar with sas commands it should look (very very roughly) like:
set var
if year = i then stdev=stdev(year(i-6) untill year(i-1)) and average = avg(year(i-6) untill year(i-1))
Or something like this, I really have no clue, I'm gonna try and figure it out but it's worth posting it if I won't find it myself.
Thanks!
Here is one way. Hope this helps.
The correct way to do it is to use PROC EXPAND.
There are a lot of options you can use with it but you might want to do
and likewise for MOVSTD. It automatically ignores missing values but you can adjust that behavior as well
I would advocate proc sql here for readability. Using Chang Chung's data as an example you could try the following: