<?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 for past 12 month returns if 9-month return data is available for mulriple fi in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Rolling-Volatility-for-past-12-month-returns-if-9-month-return/m-p/603063#M16897</link>
    <description>&lt;P&gt;I'm trying to piece together what exactly you want. Please post an example of the "want" dataset in the future. From what I understand, you want to calculate a rolling 12 month volatility for a number of stocks (but only if 9 of the 12 months are nonmissing). Adapting&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;'s response on this thread (&lt;A href="https://communities.sas.com/t5/SAS-Programming/calculating-rolling-standard-deviation-of-stock-return/td-p/488095" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/calculating-rolling-standard-deviation-of-stock-return/td-p/488095&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input stock date : yymmn6. ret;
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
1 198601 -0.04
1 198602 -0.1023
1 198603 -0.0163
1 198604 -0.14465
1 198607 -0.07383
1 198608 -0.0315
1 198609 0.05495
1 198610 -0.15368
1 198611 -0.11494
1 198612 -0.03578
1 198705 -0.1234
2 198501 -0.10435
2 198502 -0.04762
2 198503 -0.08154
2 198504 -0.06757
2 198505 -0.08911
2 198506 -0.05481
2 198507 -0.0628
2 198508 -0.0989
2 198509 -0.10811
2 198510 -0.14205
2 198511 -0.04286
2 198512 -0.05505
2 198601 -0.04
2 198602 -0.1023
2 198603 -0.0163
2 198604 -0.14465
2 198607 -0.07383
2 198608 -0.0315
2 198609 0.05495
2 198610 -0.15368
2 198611 -0.11494
2 198612 -0.03578
2 198705 -0.1234
;
run;

proc sort data=have;
by stock date;
run;

proc timeseries data=have out=have_ts;
	by stock;
	id date interval=month;
	var ret;
run;

data want;
	set have_ts;
	by stock;
	array returns(0:11) 8 _temporary_;
	if first.stock then call missing(of returns(*));
	returns(mod(_n_,12))=ret;
	if n(of returns(*))&amp;gt;9 then stdret=std(of returns(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It's best to try to fill in missing months with observations so that the calculation works properly (this is why I first create a timeseries with the have set).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-unison&lt;/P&gt;</description>
    <pubDate>Sun, 10 Nov 2019 15:07:32 GMT</pubDate>
    <dc:creator>unison</dc:creator>
    <dc:date>2019-11-10T15:07:32Z</dc:date>
    <item>
      <title>Rolling Volatility for past 12 month returns if 9-month return data is available for mulriple firm</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Rolling-Volatility-for-past-12-month-returns-if-9-month-return/m-p/603008#M16891</link>
      <description>&lt;P&gt;HI,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;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;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;the above mentioned problem was given by another user. I made modification to it by adding another stock. so can anyone help me with how can I calculate volatility for multiple&amp;nbsp;stocks?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input stock date : yymmn6. ret stdret;&lt;BR /&gt;format date yymmn6.;&lt;BR /&gt;cards;&lt;BR /&gt;1 198501 -0.10435 &lt;BR /&gt;1 198502 -0.04762 &lt;BR /&gt;1 198503 -0.08154 &lt;BR /&gt;1 198504 -0.06757 &lt;BR /&gt;1 198505 -0.08911 &lt;BR /&gt;1 198506 -0.05481 &lt;BR /&gt;1 198507 -0.0628 &lt;BR /&gt;1 198508 -0.0989 &lt;BR /&gt;1 198509 -0.10811 &lt;BR /&gt;1 198510 -0.14205 &lt;BR /&gt;1 198511 -0.04286 &lt;BR /&gt;1 198512 -0.05505 0.029861527&lt;BR /&gt;1 198601 -0.04 0.030770009&lt;BR /&gt;1 198602 -0.1023 0.030523464&lt;BR /&gt;1 198603 -0.0163 0.035403028&lt;BR /&gt;1 198604 -0.14465 0.040839661&lt;BR /&gt;1 198607 -0.07383 0.043861268&lt;BR /&gt;1 198608 -0.0315 0.046163063&lt;BR /&gt;1 198609 0.05495 0.060118401&lt;BR /&gt;1 198610 -0.15368 0.061979498&lt;BR /&gt;1 198611 -0.11494 0.063860041&lt;BR /&gt;1 198612 -0.03578 0.06457195&lt;BR /&gt;1 198705 -0.1234 &lt;BR /&gt;2 198501 -0.10435 &lt;BR /&gt;2 198502 -0.04762 &lt;BR /&gt;2 198503 -0.08154 &lt;BR /&gt;2 198504 -0.06757 &lt;BR /&gt;2 198505 -0.08911 &lt;BR /&gt;2 198506 -0.05481 &lt;BR /&gt;2 198507 -0.0628 &lt;BR /&gt;2 198508 -0.0989 &lt;BR /&gt;2 198509 -0.10811 &lt;BR /&gt;2 198510 -0.14205 &lt;BR /&gt;2 198511 -0.04286 &lt;BR /&gt;2 198512 -0.05505 0.029861527&lt;BR /&gt;2 198601 -0.04 0.030770009&lt;BR /&gt;2 198602 -0.1023 0.030523464&lt;BR /&gt;2 198603 -0.0163 0.035403028&lt;BR /&gt;2 198604 -0.14465 0.040839661&lt;BR /&gt;2 198607 -0.07383 0.043861268&lt;BR /&gt;2 198608 -0.0315 0.046163063&lt;BR /&gt;2 198609 0.05495 0.060118401&lt;BR /&gt;2 198610 -0.15368 0.061979498&lt;BR /&gt;2 198611 -0.11494 0.063860041&lt;BR /&gt;2 198612 -0.03578 0.06457195&lt;BR /&gt;2 198705 -0.1234 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;the original problem and solution from the previous post by that user is given below:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I think this solution has a problem as well. it calculates volatility for 9 months as well even if there is no missing months.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input stock date : yymmn6. ret stdret;&lt;BR /&gt;format date yymmn6.;&lt;BR /&gt;cards;&lt;BR /&gt;1 198501 -0.10435 &lt;BR /&gt;1 198502 -0.04762 &lt;BR /&gt;1 198503 -0.08154 &lt;BR /&gt;1 198504 -0.06757 &lt;BR /&gt;1 198505 -0.08911 &lt;BR /&gt;1 198506 -0.05481 &lt;BR /&gt;1 198507 -0.0628 &lt;BR /&gt;1 198508 -0.0989 &lt;BR /&gt;1 198509 -0.10811 &lt;BR /&gt;1 198510 -0.14205 &lt;BR /&gt;1 198511 -0.04286 &lt;BR /&gt;1 198512 -0.05505 0.029861527&lt;BR /&gt;1 198601 -0.04 0.030770009&lt;BR /&gt;1 198602 -0.1023 0.030523464&lt;BR /&gt;1 198603 -0.0163 0.035403028&lt;BR /&gt;1 198604 -0.14465 0.040839661&lt;BR /&gt;1 198607 -0.07383 0.043861268&lt;BR /&gt;1 198608 -0.0315 0.046163063&lt;BR /&gt;1 198609 0.05495 0.060118401&lt;BR /&gt;1 198610 -0.15368 0.061979498&lt;BR /&gt;1 198611 -0.11494 0.063860041&lt;BR /&gt;1 198612 -0.03578 0.06457195&lt;BR /&gt;1 198705 -0.1234 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *,case when (&lt;BR /&gt;(select count(ret) from have where stock=a.stock and&lt;BR /&gt;date between intnx('month',a.date,-11) and a.date) ge 9 )&lt;BR /&gt;then (select std(ret) from have where stock=a.stock and&lt;BR /&gt;date between intnx('month',a.date,-11) and a.date)&lt;BR /&gt;else . end as volatility&lt;BR /&gt;from have as a;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Nov 2019 20:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Rolling-Volatility-for-past-12-month-returns-if-9-month-return/m-p/603008#M16891</guid>
      <dc:creator>Takdir</dc:creator>
      <dc:date>2019-11-09T20:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling Volatility for past 12 month returns if 9-month return data is available for mulriple fi</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Rolling-Volatility-for-past-12-month-returns-if-9-month-return/m-p/603063#M16897</link>
      <description>&lt;P&gt;I'm trying to piece together what exactly you want. Please post an example of the "want" dataset in the future. From what I understand, you want to calculate a rolling 12 month volatility for a number of stocks (but only if 9 of the 12 months are nonmissing). Adapting&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;'s response on this thread (&lt;A href="https://communities.sas.com/t5/SAS-Programming/calculating-rolling-standard-deviation-of-stock-return/td-p/488095" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/calculating-rolling-standard-deviation-of-stock-return/td-p/488095&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input stock date : yymmn6. ret;
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
1 198601 -0.04
1 198602 -0.1023
1 198603 -0.0163
1 198604 -0.14465
1 198607 -0.07383
1 198608 -0.0315
1 198609 0.05495
1 198610 -0.15368
1 198611 -0.11494
1 198612 -0.03578
1 198705 -0.1234
2 198501 -0.10435
2 198502 -0.04762
2 198503 -0.08154
2 198504 -0.06757
2 198505 -0.08911
2 198506 -0.05481
2 198507 -0.0628
2 198508 -0.0989
2 198509 -0.10811
2 198510 -0.14205
2 198511 -0.04286
2 198512 -0.05505
2 198601 -0.04
2 198602 -0.1023
2 198603 -0.0163
2 198604 -0.14465
2 198607 -0.07383
2 198608 -0.0315
2 198609 0.05495
2 198610 -0.15368
2 198611 -0.11494
2 198612 -0.03578
2 198705 -0.1234
;
run;

proc sort data=have;
by stock date;
run;

proc timeseries data=have out=have_ts;
	by stock;
	id date interval=month;
	var ret;
run;

data want;
	set have_ts;
	by stock;
	array returns(0:11) 8 _temporary_;
	if first.stock then call missing(of returns(*));
	returns(mod(_n_,12))=ret;
	if n(of returns(*))&amp;gt;9 then stdret=std(of returns(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It's best to try to fill in missing months with observations so that the calculation works properly (this is why I first create a timeseries with the have set).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-unison&lt;/P&gt;</description>
      <pubDate>Sun, 10 Nov 2019 15:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Rolling-Volatility-for-past-12-month-returns-if-9-month-return/m-p/603063#M16897</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-11-10T15:07:32Z</dc:date>
    </item>
  </channel>
</rss>

