How can I calculate the standard deviation of returns over the past 12 months? I have a dataset of daily observations. I have a yearmonth variable. I can use PROC EXPAND and get a rolling standard deviation over the past 252 days using the following code:
PROC EXPAND DATA=daily_returns OUT=daily_returns2;
CONVERT return=std / TRANSFORMOUT=(MOVSTD 252);
by stock;
RUN;
This would work for most cases, but what if a stock halts trading for several months, this would include returns of days that are more than 1 year ago. I want to use only observations that are strictly within the past 12 months.
Another related issue is that, say a stock stops trading for more than a year, and then reappear. These days without trading would still appear in my dataset with missing values for returns. However, my code still returns the rolling standard deviation values for these observations when it should not. How can I solve this?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: