DATA Step, Macro, Functions and more

Calculating rolling 6-month skewness from daily returns

Reply
Senior User
Posts: 1

Calculating rolling 6-month skewness from daily returns

I have data of daily returns for several stocks. This is what my data looks like (simplified):

 

PERMNODATERETURN
100782010JAN020.0500
101042010JAN02-0.0190
101072010JAN020.0020
100782010JAN030.0040
101042010JAN03-0.0400
101072010JAN030.0500
.........
100782015JAN02-0.0190
101042015JAN020.0100
101072015JAN020.0700
100782015JAN050.0500
101042010JAN03-0.0190
101072010JAN030.0020

 

PERMNO identifies a stock, DATE identifies a date (yyyymmmdd), RETURN is the daily stock return.

I have simplified this example for only three stocks (10078, 10104, 10107).

 

Goal: I am trying to calculate rolling skewness for each stock i in a given month t.

I want to calculate the monthly skewness measure for each stock using the previous 6 months (i.e. months t-6 to t-1) of daily returns data. Therefore, for a stock in e.g. July 2010, I want the skewness measure for that month to be based on its daily returns from January 2010 to June 2010.

 

I want the output data to include PERMNO, month ID, and the monthly skewness measure (based on prior 6 months of data) for that month. Here is a a picture to illustrate the desired output I want:

 

PERMNODATE6MONTH_SKEWNESS
100782010JUL300.7257
101042010JUL30-0.7056
101072010JUL30-0.6781
100782010AUG310.9999
101042010AUG31-0.6719
101072010AUG31-0.7056
.........
100782015JUL30-0.1651
101042015JUL300.1056
101072015JUL300.6181
100782015AUG31-0.8886
101042015AUG310.6119
101072015AUG310.1056

 

 

I have searched the web extensively and tried this myself, but I feel really stuck on this problem. Thank you in advance for anyone who is able to help in any way.

Super User
Posts: 23,262

Re: Calculating rolling 6-month skewness from daily returns

Posted in reply to xplosive111

If your data is continuous you can use something like the following:

https://gist.github.com/statgeek/07a3708dee1225ceb9d4aa75daab2c52

 

If you don't have continuous data or PROC EXPAND here is another method:

https://communities.sas.com/t5/SAS-Statistical-Procedures/Moving-Average-with-Multiple-Observations/...

 

You'll have to modify either for skewness, but that doesn't even have a standard definition so make sure the ones in SAS are what you want. 

Trusted Advisor
Posts: 1,309

Re: Calculating rolling 6-month skewness from daily returns

Posted in reply to xplosive111
data want;
  set have;
  by permno ;
  obs+1;
  if first.permno then obs=1;
  
  array ret_hist{0:59} _temporary_;
  if obs>=61 then do;
    sk=skewness(of ret_hist{*});
    output;
  end;
  ret_hist{mod(obs,60)}=return;
 run;

 

This calculates lagged skewness for every window of size 60, by applying the skewness function against a 60-element history of returns.  The array RET_HIST contains returns for date{i-60} through date{i-1}.  Only after skewness is calculated is the history updated with returns for date{i} (replacing returns of date{i-60}).  This establishes an array for dates{i-59} through date{i}, ready for the next incoming obs.

 

The program is slightly inefficient since each skewness is calculated from scratch.   One could calculate skewness of a window from (a) skewness of the previous windows and the returns from the (b) new return in the window (actually the return for date{i-1}) and the (c) dropped return (for date{I-61}).  Come back to us if you need that efficiency, although I doubt it would make that much difference.

Ask a Question
Discussion stats
  • 2 replies
  • 242 views
  • 0 likes
  • 3 in conversation