title: Calculating moving average/stdev in SAS?
body: <p>Hye guys,</p>
  
  <p>I included a screenshot to help clarify my problem:</p>
  
  <p><a href="http://i40.tinypic.com/mcrnmv.jpg" rel="nofollow noreferrer">http://i40.tinypic.com/mcrnmv.jpg</a>.</p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>As i'm not very familiar with sas commands it should look (very very roughly) like:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks!</p>