<?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: Rolling Volatility based on past 12 month returns if 9-month return data is available in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291490#M60396</link>
    <description>Its like 4 million observations. I am running the code and its taking ages to provide results.&lt;BR /&gt;</description>
    <pubDate>Sat, 13 Aug 2016 07:10:46 GMT</pubDate>
    <dc:creator>MAC1430</dc:creator>
    <dc:date>2016-08-13T07:10:46Z</dc:date>
    <item>
      <title>Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291457#M60378</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Dear all,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I want to calculate rolling volatility based on past 12 month returns i.e., from July 1997 to June 1998. &amp;nbsp;Furthermore, if a month is missing in past 12 months then volatility calculation should be based on 11 months &amp;nbsp;between the period of July 1997 to June 1998 instead of using one earlier month i.e, June 1997.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Thanks a lot for your time.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;MAC&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291457#M60378</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T03:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291466#M60380</link>
      <description>&lt;P&gt;Please provide sample data as as SAS Data step creating such data. Then explain us how the expected result should look like (also as an example using data).&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291466#M60380</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-08-13T03:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291468#M60381</link>
      <description>&lt;P&gt;How are you defining volatility?&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291468#M60381</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-13T03:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291470#M60382</link>
      <description>&lt;P&gt;Thanks a lot for your prompt response. Sorry, I did not explain it earlier. I want to calculate volatility using stanadard deviation over past months only if there are atleast 9 month observations. I have attached an excel sheet for illustration. For example in Dec 1985, I calculate volatility based on monthly returns from January-December 1985 and it has data for all 12 months in 1985. However,&amp;nbsp;&lt;SPAN&gt;I calculate volatility based on monthly returns from August 1985 to July 1985 but&amp;nbsp;it has data for 10 months in a 12 month period. I do not calculate volatility for May1987 because it does not have 9 month observations in past 12 months i.e from June 1986 to May 1987.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291470#M60382</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T03:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291471#M60383</link>
      <description>&lt;P&gt;Dear Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thansk for your prompt response. I am defining volatility based on standard deviation form past 12 month returns. I have attached the file as well.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291471#M60383</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T03:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291472#M60384</link>
      <description>&lt;P&gt;Do you have a SAS ETS licence? If so, look at proc Expand. The documentation, example on transformations, is what you're looking for.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291472#M60384</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-13T03:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291473#M60385</link>
      <description>&lt;P&gt;Dear Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks. YEs, I have SAS license and I used it to calculate volatility. But the problem is that it calculates based on past 12 observations instead of just past 12 months. For example, if one month is missing in past 12 months period i.e., July is missing from January to December 1985 then it will use monthly return from December 1984 too. I am using teh following code in SAS:&lt;/P&gt;
&lt;P&gt;Data ret;&lt;BR /&gt;infile 'D:\My SAS Files\9.4\US\US paper data\ret.dat';&lt;BR /&gt;Input stock $1-5 date1 $7-14 exc $16-17 sharecode $19-20 ret;&lt;BR /&gt;run;&lt;BR /&gt;data ret1;&lt;BR /&gt;set ret;&lt;BR /&gt;if sharecode^=10 and sharecode^=11 then delete;&lt;BR /&gt;date=SUBSTRN(date1,1,6);&lt;BR /&gt;year= SUBSTRN(date1,1,4);&lt;BR /&gt;month= SUBSTRN(date1,5,2);&lt;BR /&gt;run;&lt;BR /&gt;data have(keep=stock ret monthDate);&lt;BR /&gt;set ret1;&lt;BR /&gt;monthDate = mdy(month, 1, year);&lt;BR /&gt;if ret=-66 then delete;&lt;BR /&gt;if ret=-77 then delete;&lt;BR /&gt;if ret=-88 then delete;&lt;BR /&gt;if ret=-99 then delete;&lt;BR /&gt;if ret=. then delete;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table roll as&lt;BR /&gt;select&lt;BR /&gt; h2.stock, &lt;BR /&gt; h2.monthDate as periodEndDate format=yymmd7., &lt;BR /&gt; h2.ret,&lt;BR /&gt; mean(h1.ret) as meanRet,&lt;BR /&gt; std(h1.ret) as stdRet&lt;BR /&gt;from &lt;BR /&gt; have as h1 inner join &lt;BR /&gt; have as h2 on h1.stock=h2.stock and &lt;BR /&gt; intck("MONTH", h1.monthDate, h2.monthDate) between 0 and 11&lt;BR /&gt;group by h2.stock, h2.monthDate, h2.ret&lt;BR /&gt;having count(h2.stock)=12;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 03:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291473#M60385</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T03:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291480#M60387</link>
      <description>&lt;P&gt;SAS ETS licence. SAS licences different modules that do different things. ETS is Econometrics and Time Series which handles serial data such as time and date, and is capable of filling in missing periods or accounting for it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your query looks close, except for your having clause which requires that you have 12 months of data. This is contradictory to what is specified in your requirements, I think it's 9 now?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 05:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291480#M60387</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-13T05:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291481#M60388</link>
      <description>&lt;PRE&gt;
You'd better post your data at this forum, not attachment. No one would like to download it .


data have;
infile cards expandtabs truncover;
input stock	date : yymmn6.	ret	stdret;
format date yymmn6.;
cards;
1	198501	-0.10435	
1	198502	-0.04762	
1	198503	-0.08154	
1	198504	-0.06757	
1	198505	-0.08911	
1	198506	-0.05481	
1	198507	-0.0628	
1	198508	-0.0989	
1	198509	-0.10811	
1	198510	-0.14205	
1	198511	-0.04286	
1	198512	-0.05505	0.029861527
1	198601	-0.04	0.030770009
1	198602	-0.1023	0.030523464
1	198603	-0.0163	0.035403028
1	198604	-0.14465	0.040839661
1	198607	-0.07383	0.043861268
1	198608	-0.0315	0.046163063
1	198609	0.05495	0.060118401
1	198610	-0.15368	0.061979498
1	198611	-0.11494	0.063860041
1	198612	-0.03578	0.06457195
1	198705	-0.1234	
;
run;
proc sql;
create table want as
 select *,case when (
 (select count(ret) from have where stock=a.stock and
  date between intnx('month',a.date,-11) and a.date) ge 9 )
then (select std(ret) from have where stock=a.stock and
  date between intnx('month',a.date,-11) and a.date)
  else . end as volatility
  from have as a;
quit;


&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Aug 2016 05:16:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291481#M60388</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-13T05:16:44Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291482#M60389</link>
      <description>&lt;P&gt;Thanks a lot, its really helpful. I will post data in future if I have a question.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 05:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291482#M60389</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T05:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291483#M60390</link>
      <description>&lt;P&gt;Thanks a lot for your help, another member posted the solution I was looking for.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 05:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291483#M60390</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T05:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291487#M60394</link>
      <description>&lt;P&gt;I just found a small problem with it, the code works very well for the samll data-set but it stops working for large data-set. Any idea how to fix the code to work for a large data-set? Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 06:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291487#M60394</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T06:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291489#M60395</link>
      <description>&lt;P&gt;What does stop working mean? Why does it stop working?&lt;/P&gt;
&lt;P&gt;Whats a large data set?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 07:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291489#M60395</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-13T07:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291490#M60396</link>
      <description>Its like 4 million observations. I am running the code and its taking ages to provide results.&lt;BR /&gt;</description>
      <pubDate>Sat, 13 Aug 2016 07:10:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291490#M60396</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T07:10:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291501#M60398</link>
      <description>&lt;PRE&gt;
OK. If you have a big table, Try Hash Table. 
If the following code still couldn't work. I can use DOW+Hash Table .



data have;
infile cards expandtabs truncover;
input stock	date : yymmn6.	ret	stdret;
format date yymmn6.;
cards;
1	198501	-0.10435	
1	198502	-0.04762	
1	198503	-0.08154	
1	198504	-0.06757	
1	198505	-0.08911	
1	198506	-0.05481	
1	198507	-0.0628	
1	198508	-0.0989	
1	198509	-0.10811	
1	198510	-0.14205	
1	198511	-0.04286	
1	198512	-0.05505	0.029861527
1	198601	-0.04	0.030770009
1	198602	-0.1023	0.030523464
1	198603	-0.0163	0.035403028
1	198604	-0.14465	0.040839661
1	198607	-0.07383	0.043861268
1	198608	-0.0315	0.046163063
1	198609	0.05495	0.060118401
1	198610	-0.15368	0.061979498
1	198611	-0.11494	0.063860041
1	198612	-0.03578	0.06457195
1	198705	-0.1234	
;
run;
data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h(dataset:'have',hashexp:20);
  h.definekey('stock','date');
  h.definedata('ret');
  h.definedone();
 end;
set have;
array x{12} _temporary_;
n=0;
call missing(of x{*});

do i=-11 to 0;
 temp=intnx('month',date,i);
 rc=h.find(key:stock,key:temp);
 if rc=0 then do;n+1;x{n}=ret;end;
end;

if n ge 9 then volatility=std(of x{*});
 else volatility=.;
drop i n temp rc;
run;


&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Aug 2016 08:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291501#M60398</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-13T08:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility based on past 12 month returns if 9-month return data is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291502#M60399</link>
      <description>&lt;P&gt;Thanks a lot for it, its working. I am new to this community, will try to provide more details in the future. Enjoy the weekend.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Aug 2016 08:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-Volatility-based-on-past-12-month-returns-if-9-month/m-p/291502#M60399</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-08-13T08:38:40Z</dc:date>
    </item>
  </channel>
</rss>

