<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating rolling 6-month skewness from daily returns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427556#M105474</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by permno ;
  obs+1;
  if first.permno then obs=1;
  
  array ret_hist{0:59} _temporary_;
  if obs&amp;gt;=61 then do;
    sk=skewness(of ret_hist{*});
    output;
  end;
  ret_hist{mod(obs,60)}=return;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This calculates lagged skewness for every window of size 60, by applying the skewness function against a 60-element history of returns.&amp;nbsp; The array RET_HIST contains returns for date{i-60} through date{i-1}.&amp;nbsp; Only after skewness is calculated is the history updated with returns for date{i} (replacing returns of date{i-60}).&amp;nbsp; This establishes an array for dates{i-59} through date{i}, ready for the next incoming obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program is slightly inefficient since each skewness is calculated from scratch.&amp;nbsp;&amp;nbsp; 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}).&amp;nbsp; Come back to us if you need that efficiency, although I doubt it would make that much difference.&lt;/P&gt;</description>
    <pubDate>Sun, 14 Jan 2018 21:33:26 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-01-14T21:33:26Z</dc:date>
    <item>
      <title>Calculating rolling 6-month skewness from daily returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427551#M105470</link>
      <description>&lt;P&gt;I have data of daily returns for several stocks. This is what my data looks like (simplified):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;PERMNO&lt;/TD&gt;&lt;TD&gt;DATE&lt;/TD&gt;&lt;TD&gt;RETURN&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10078&lt;/TD&gt;&lt;TD&gt;2010JAN02&lt;/TD&gt;&lt;TD&gt;0.0500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10104&lt;/TD&gt;&lt;TD&gt;2010JAN02&lt;/TD&gt;&lt;TD&gt;-0.0190&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10107&lt;/TD&gt;&lt;TD&gt;2010JAN02&lt;/TD&gt;&lt;TD&gt;0.0020&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10078&lt;/TD&gt;&lt;TD&gt;2010JAN03&lt;/TD&gt;&lt;TD&gt;0.0040&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10104&lt;/TD&gt;&lt;TD&gt;2010JAN03&lt;/TD&gt;&lt;TD&gt;-0.0400&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10107&lt;/TD&gt;&lt;TD&gt;2010JAN03&lt;/TD&gt;&lt;TD&gt;0.0500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10078&lt;/TD&gt;&lt;TD&gt;2015JAN02&lt;/TD&gt;&lt;TD&gt;-0.0190&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10104&lt;/TD&gt;&lt;TD&gt;2015JAN02&lt;/TD&gt;&lt;TD&gt;0.0100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10107&lt;/TD&gt;&lt;TD&gt;2015JAN02&lt;/TD&gt;&lt;TD&gt;0.0700&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10078&lt;/TD&gt;&lt;TD&gt;2015JAN05&lt;/TD&gt;&lt;TD&gt;0.0500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10104&lt;/TD&gt;&lt;TD&gt;2010JAN03&lt;/TD&gt;&lt;TD&gt;-0.0190&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10107&lt;/TD&gt;&lt;TD&gt;2010JAN03&lt;/TD&gt;&lt;TD&gt;0.0020&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PERMNO&amp;nbsp;identifies a stock,&amp;nbsp;DATE&amp;nbsp;identifies a date (yyyymmmdd),&amp;nbsp;RETURN&amp;nbsp;is the daily stock return.&lt;/P&gt;&lt;P&gt;I have simplified this example for only three stocks (10078, 10104, 10107).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Goal: I am trying to calculate rolling skewness for each stock i in a given month t.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;PERMNO&lt;/TD&g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2015JUL30&lt;/TD&gt;&lt;TD&gt;0.6181&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10078&lt;/TD&gt;&lt;TD&gt;2015AUG31&lt;/TD&gt;&lt;TD&gt;-0.8886&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10104&lt;/TD&gt;&lt;TD&gt;2015AUG31&lt;/TD&gt;&lt;TD&gt;0.6119&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10107&lt;/TD&gt;&lt;TD&gt;2015AUG31&lt;/TD&gt;&lt;TD&gt;0.1056&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 20:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427551#M105470</guid>
      <dc:creator>xplosive111</dc:creator>
      <dc:date>2018-01-14T20:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling 6-month skewness from daily returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427554#M105472</link>
      <description>&lt;P&gt;If your data is continuous you can use something like the following:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/07a3708dee1225ceb9d4aa75daab2c52" target="_blank"&gt;https://gist.github.com/statgeek/07a3708dee1225ceb9d4aa75daab2c52&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't have continuous data&amp;nbsp;or PROC EXPAND here is another method:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Statistical-Procedures/Moving-Average-with-Multiple-Observations/m-p/392693/highlight/true#M20488" target="_blank"&gt;https://communities.sas.com/t5/SAS-Statistical-Procedures/Moving-Average-with-Multiple-Observations/m-p/392693/highlight/true#M20488&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 21:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427554#M105472</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-14T21:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling 6-month skewness from daily returns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427556#M105474</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by permno ;
  obs+1;
  if first.permno then obs=1;
  
  array ret_hist{0:59} _temporary_;
  if obs&amp;gt;=61 then do;
    sk=skewness(of ret_hist{*});
    output;
  end;
  ret_hist{mod(obs,60)}=return;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This calculates lagged skewness for every window of size 60, by applying the skewness function against a 60-element history of returns.&amp;nbsp; The array RET_HIST contains returns for date{i-60} through date{i-1}.&amp;nbsp; Only after skewness is calculated is the history updated with returns for date{i} (replacing returns of date{i-60}).&amp;nbsp; This establishes an array for dates{i-59} through date{i}, ready for the next incoming obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program is slightly inefficient since each skewness is calculated from scratch.&amp;nbsp;&amp;nbsp; 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}).&amp;nbsp; Come back to us if you need that efficiency, although I doubt it would make that much difference.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Jan 2018 21:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-6-month-skewness-from-daily-returns/m-p/427556#M105474</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-01-14T21:33:26Z</dc:date>
    </item>
  </channel>
</rss>

