0

Relatively new to SAS here and I am having trouble calculating rolling standard deviation for equity stock returns.

I have a table named "stocks_ret" which contains 3 columns: date, stock number, returns. The data is monthly.

I want to calculate the standard deviation of returns for the past 36 months for each stock.

The final table should contain 4 columns: date, stock number, returns, rolling standard deviation. Observations without a standard deviation value should be removed (i.e. observations less than 36 months old will not appear in the final table)

Please help! Thanks!

ssxdots
  • 388
  • 1
  • 3
  • 7
  • Here's a another request of the same question, with data step solutions. http://stackoverflow.com/questions/28221628/algorithm-for-calculating-most-stable-consecutive-values-from-a-database – Reeza Feb 01 '15 at 19:04
  • thanks! its slightly different but it's more elegant than proc sql method. – ssxdots Feb 02 '15 at 18:21

1 Answers1

1

@Reeza posted a link in the comments to an answer with PROC EXPAND. For that, you will need SAS/ETS licensed.

For a pure Base SAS way, there are a number of options. This blog posts talks about some (not all) of them and the tradeoffs. It also provides sample code. http://statsadventure.blogspot.com/2012/08/rolling-summary-stats-in-sas.html

DomPazz
  • 12,415
  • 17
  • 23
  • I actually attempted the macro (method 4) in the blog first. it is indeed very efficient but i couldn't adapt it properly for my data because the logic is very hard to follow for a novice like me. nonetheless, that blog post teaches a lot esp for beginners like me. – ssxdots Feb 02 '15 at 18:24