BookmarkSubscribeRSS Feed
xplosive111
Calcite | Level 5

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.

2 REPLIES 2
Reeza
Super User

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. 

mkeintz
PROC Star
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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1594 views
  • 0 likes
  • 3 in conversation