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?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.