<?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 calculating standard deviations within certain intervals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/698919#M213784</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know it is not hard but now facing a tricky case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
 select *,(select std(x) from have where year between a.year-2 and a.year and code=a.code) as rolling_std
  from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I can use something as above but the thing is that I am now working with a date like 1988/05/05 instead of just year like shown in the example code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate standard deviations of observations over certain periods after certain events occur.&lt;/P&gt;&lt;P&gt;For example,&amp;nbsp;&lt;/P&gt;&lt;P&gt;event date:1988/05/16&lt;/P&gt;&lt;P&gt;observations: MONTHLY stock returns&lt;/P&gt;&lt;P&gt;period: last 3 years before the event occurs&lt;/P&gt;&lt;P&gt;what I want: standard deviation of MONTHLY returns from 1985/05/15~1988/05/15, conceptually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when it comes to doing this kind of job, I certainly recognize there would be the case that there is no observation on both or either starting date or ending date(like&amp;nbsp;1988/05/15 here in this example), so I also need to handle this issue as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I think doing hard are&lt;/P&gt;&lt;P&gt;1. how to set month interval in the PROC SQL while I am using not "year" variable but "date" variable. I do not think I can just write date-365 or something. If so... well, I have thought too much&lt;/P&gt;&lt;P&gt;2. how to locate the closest alternative dates when there is no exact&amp;nbsp;starting date or ending date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 14 Nov 2020 22:53:48 GMT</pubDate>
    <dc:creator>JKCho</dc:creator>
    <dc:date>2020-11-14T22:53:48Z</dc:date>
    <item>
      <title>calculating standard deviations within certain intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/698919#M213784</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know it is not hard but now facing a tricky case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
 select *,(select std(x) from have where year between a.year-2 and a.year and code=a.code) as rolling_std
  from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I can use something as above but the thing is that I am now working with a date like 1988/05/05 instead of just year like shown in the example code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate standard deviations of observations over certain periods after certain events occur.&lt;/P&gt;&lt;P&gt;For example,&amp;nbsp;&lt;/P&gt;&lt;P&gt;event date:1988/05/16&lt;/P&gt;&lt;P&gt;observations: MONTHLY stock returns&lt;/P&gt;&lt;P&gt;period: last 3 years before the event occurs&lt;/P&gt;&lt;P&gt;what I want: standard deviation of MONTHLY returns from 1985/05/15~1988/05/15, conceptually.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when it comes to doing this kind of job, I certainly recognize there would be the case that there is no observation on both or either starting date or ending date(like&amp;nbsp;1988/05/15 here in this example), so I also need to handle this issue as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I think doing hard are&lt;/P&gt;&lt;P&gt;1. how to set month interval in the PROC SQL while I am using not "year" variable but "date" variable. I do not think I can just write date-365 or something. If so... well, I have thought too much&lt;/P&gt;&lt;P&gt;2. how to locate the closest alternative dates when there is no exact&amp;nbsp;starting date or ending date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Nov 2020 22:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/698919#M213784</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-14T22:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: calculating standard deviations within certain intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/698925#M213789</link>
      <description>&lt;P&gt;Show us the code with months that you have tried.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;I do not think I can just write date-365 or something. If so... well, I have thought too much&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Well, actually, that should work if you do it properly.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how to locate the closest alternative dates when there is no exact starting date or ending date.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think this has been explained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Nov 2020 00:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/698925#M213789</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-15T00:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: calculating standard deviations within certain intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699034#M213834</link>
      <description>&lt;P&gt;Hi JkCho,&lt;BR /&gt;It seems you want to filter by month while data resides in date and do standard deviation calc while it starts or ends in the middle of month accordingly.&lt;BR /&gt;If that is the case, you might want to use intck function to do interval calculation and group it and filter out month start or end cases using case clause in SQL.&lt;/P&gt;
&lt;P&gt;intck function&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=casfedsql&amp;amp;docsetTarget=n096g3nwx05g7wn161ax2rdqmlfn.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=vdmmlcdc&amp;amp;cdcVersion=8.1&amp;amp;docsetId=casfedsql&amp;amp;docsetTarget=n096g3nwx05g7wn161ax2rdqmlfn.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;case expression&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=casfedsql&amp;amp;docsetTarget=n0t7379re7ydjcn1rg93rlne17wt.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=casfedsql&amp;amp;docsetTarget=n0t7379re7ydjcn1rg93rlne17wt.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;There was a similar thread in the past.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/rolling-standard-deviation-calculation/m-p/160026#M41708" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/rolling-standard-deviation-calculation/m-p/160026#M41708&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Below is a sample code borrowed from thread above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*sample data;
data have;
	format date date9. ;
		do code=1 to 4;
			do date="15May1980"d to "15May1999"d;
				ret=date;
				output;
			end;
		end;
run;

*create view starting from 1985 to 1988;
proc sql ;
create view _want as
select 
	 put(date,yymmn.) as ym,
	 date,
	 code,
	/*check interval and filter with case */
	 case when date &amp;lt; "15May1985"d
	   then .
	   when date &amp;gt; "15May1988"d then .
	   /*intck function option "C" specifies that continuous time is measured*/
	   else intck('month', "15May1985"d, date,'C')
	 end as interval,
	 ret
	  from have a
	  		/*filter 3 year interval starting from 15May1985 to 15May1988*/
	  		having interval between 0 and 36 and "15May1985"d &amp;lt;=date &amp;lt;= "15May1988"d
	  			order by code,ym,date;
quit;
*rolling standard deviation;
proc sql ;
create table want as select *, (select std(ret) from _want where interval between a.interval-36 and a.interval-1 and code=a.code) as rolling_std
from _want as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Nov 2020 05:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699034#M213834</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-16T05:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: calculating standard deviations within certain intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699418#M213961</link>
      <description>oh. Yeah... Thank you so much. I was working on a similar code and realized that I have asked the question here!&lt;BR /&gt;Yes. your code works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;Thank you again!</description>
      <pubDate>Tue, 17 Nov 2020 09:26:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699418#M213961</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-17T09:26:23Z</dc:date>
    </item>
    <item>
      <title>Re: calculating standard deviations within certain intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699419#M213962</link>
      <description>&lt;P&gt;oh wow...&lt;/P&gt;
&lt;P&gt;Never thought my reply would solve your problem.&lt;/P&gt;
&lt;P&gt;Personally, I learned a lot about stock analysis and this was fun!&lt;/P&gt;
&lt;P&gt;Have a good day!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 09:29:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-standard-deviations-within-certain-intervals/m-p/699419#M213962</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-17T09:29:25Z</dc:date>
    </item>
  </channel>
</rss>

