<?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: Standard deviation on rolling basis in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/753072#M237303</link>
    <description>&lt;P&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;I am going to calculate sigma following the paper Campbell et al. (2008), where he defines sigma as &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;Sigma_t-1,t-2,t-3=SQRT(252*(1/N-1) sum(r^2_i,k)) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;Where r^2 is the firm-level daily return and N is the number of trading days in three months period. K is the index of trading days in months t-1, t-2, t-3. They require a firm have at least 5 nonzero daily observations. In this case, they report sigma as missing and replace it with the annual cross-sectional mean. &lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;I have the attached code, it seems it is modified from your code in this question (I do not have much idea of such hash function), can you look into this and tell me whether the 3 months rolling volatility/standard deviation calculated perfectly or not and how can I annualize it? I am attaching the code for you. If you think there is any other efficient way or the code need modification please post it. Thanks in advance.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;The link of the paper which I am following &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;&lt;A href="https://scholar.harvard.edu/files/campbell/files/campbellhilscherszilagyi_jf2008.pdf" target="_blank" rel="noopener"&gt;https://scholar.harvard.edu/files/campbell/files/campbellhilscherszilagyi_jf2008.pdf&lt;/A&gt; (page 2936)&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data crsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set work.daily_return;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;format date mmddyy10.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;month=month(date);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;keep permno date month ret;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data have ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set crsp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;ret=input(ret,?? best32.);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;drop Returns;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;monyy=intnx('month',Date,0);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;format monyy yymmn6.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;proc sort data=have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by PERMNO monyy Date;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if _n_=1 then do;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if 0 then set have(rename=(ret=_ret));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;declare hash h(multidata:'y');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;h.definekey('Date');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;h.definedata('_ret');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;h.definedone();&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;array x{100} _temporary_;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;do until(last.PERMNO);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by PERMNO;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;_ret=ret;h.add();&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;do until(last.PERMNO);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by PERMNO;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;n=0;call missing(of x{*});&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;m.clear(); /**&amp;lt;---***/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;do i=intnx('month',monyy,-3) to intnx('month',monyy,-1,'e');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;rc=h.find(key:i);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;do while(rc=0);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if not missing(_RET) then do;n+1;x{n}=_ret; _monyy=intnx('month',i,0); m.replace(); end; /**&amp;lt;---***/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;rc=h.find_next(key:i);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;std=std(of x{*});&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if n&amp;lt;5 then std=.; /**&amp;lt;-----****/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if m.num_items&amp;lt;3 then std=.; /**&amp;lt;----EDIT HERE-****/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;output;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;h.clear();&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;drop i _ret n rc;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run; &lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data trim;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;date2=intnx('month',date,1)-1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;date1=intnx('month',date2,0);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;date_of=intnx('month',date1,-3);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;year=year(date);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;format date1 date2 date_of date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;drop date1 monyy _monyy ret;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;proc sort data=trim;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by permno month year;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data monthly;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set trim;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by permno month year;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;firstpermno=first.permno;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;lastpermno=last.permno;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;firstyear=first.year;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;lastyear=last.year;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;*firstmonth=first.month;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;*lastmonth=last.month;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if lastyear=1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;proc sort data=monthly;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by permno date;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;data sigma;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;set monthly;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=1 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=2 and year(date)in ('2000','2004','2008','2012','2016') then date=mdy(2,29,year(date)); &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;else if month=2 and year(date) not in ('2000','2004','2008','2012','2016') then date=mdy(2,28,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=3 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=4 then date=mdy(month,30,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=5 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=6 then date=mdy(month,30,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=7 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=8 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=9 then date=mdy(month,30,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=10 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=11 then date=mdy(month,30,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;if month=12 then date=mdy(month,31,year(date));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;drop firstpermno lastpermno lastyear firstyear;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;by permno date;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;/* Annualizing Sigma */&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;Data Sigma2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;Set Sigma;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;Sigma_Annualized= sqrt(252/(64-1))*three months rolling volatility;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jul 2021 07:13:49 GMT</pubDate>
    <dc:creator>Ramin1</dc:creator>
    <dc:date>2021-07-09T07:13:49Z</dc:date>
    <item>
      <title>Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446482#M112023</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;&lt;P&gt;I have daily returns data for firms. I would like to calculate the standard deviation of the returns (say, sigma) from the daily observations for the last 3 months on a rolling basis. For example, sigma for for firm i in december 2017 will be the standard deviations of the daily returns from september to november 2017. Similarly, sigma for firm i in november 2017 will be the standard deviations of the daily returns from august to october 2017.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Abu&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 20:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446482#M112023</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-17T20:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446493#M112026</link>
      <description>&lt;P&gt;Can you post some sample data? Have you tried anything so far as well? PROC EXPAND is probably where I'd start, but I'd also consider changing your definition. If you use the previous 90 days for example or X trading days. 3 months is not a standard measure and you'll see blips because of February issue alone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 22:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446493#M112026</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-17T22:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446555#M112048</link>
      <description>&lt;PRE&gt;

proc sql;
create table want as
 select *,(select std(return) from have where id=a.id and date between intnx('month',a.date,-3) and a.date ) as std
   from have as a;
quit;

&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 Mar 2018 10:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446555#M112048</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-18T10:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446573#M112060</link>
      <description>&lt;P&gt;Thank you Xia. But proc sql takes very long time if there are millions of observations. Is there any other way such as using array or using macro?&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 14:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446573#M112060</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-18T14:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446586#M112067</link>
      <description>&lt;P&gt;Yes, of course calculations of this nature are going to take a very long time if you have millions of observations. If speed doing the calculation on millions of observations is a concern, you should have stated this at the beginning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please read what Reeza said and show us some sample data, and show us the desired results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 16:16:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446586#M112067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-03-18T16:16:25Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446598#M112074</link>
      <description>&lt;P&gt;Ksharp's (Xia) code is ok but I would like to know if it's possible to do using macro or using array. I attach the sample dataset. Test file is attached since sas data file cannot be attached.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 17:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446598#M112074</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-18T17:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446610#M112078</link>
      <description>&lt;P&gt;Yes, of course it would be possible to write this as a macro. I don't know if it would be faster on millions of observations because I never tried. As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; has stated, PROC EXPAND might also be a good solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, there is this solution, which works for regression coefficients with PROC REG, but something similar using PROC SUMMARY ought to work for standard deviations &lt;A href="https://communities.sas.com/t5/SAS-Procedures/How-to-capture-the-regression-coeffecient-for-certain-time/m-p/445977#M69436" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/How-to-capture-the-regression-coeffecient-for-certain-time/m-p/445977#M69436&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;(and this could be customized to handle the different number of days in each month)&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 20:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446610#M112078</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-03-18T20:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446612#M112079</link>
      <description>&lt;P&gt;If it was a fixed interval a different approach would be easier but since the number of&amp;nbsp;changes each month depending on the month that's the biggest problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could probably expand the temporary array approach used here, but PROC EXPAND is still your best option. Have you looked into that at all? It would likely also be the fastest option as SAS would have optimized it already.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/41/380.html" target="_blank"&gt;http://support.sas.com/kb/41/380.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would need to set the array size to the largest possible number of records, probably 93 to be safe.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2018 20:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446612#M112079</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-18T20:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446798#M112166</link>
      <description>&lt;P&gt;I think Hash Table could do that. Post some data ,maybe I could give a try.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 14:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446798#M112166</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-19T14:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446860#M112192</link>
      <description>&lt;P&gt;Hi Xia,&lt;/P&gt;&lt;P&gt;Sample data is attached in text format. Please try with hash object.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2018 16:51:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/446860#M112192</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-19T16:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447089#M112248</link>
      <description>&lt;P&gt;OK. Try this one. Notice: each PERMNO and NAMES_DATE have only one Return.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile='c:\temp\daily_ret.txt' out=have dbms=tab replace;
guessingrows=32767;
run;
data have ;
 set have;
 ret=input(Returns,?? best32.);
 drop Returns;
 monyy=intnx('month',Names_Date,0);
 format monyy yymmn6.;
run;
proc sort data=have;by PERMNO monyy Names_Date;run;

data want;
 if _n_=1 then do;
   if 0 then set have(rename=(ret=_ret));
   declare hash h();
   h.definekey('Names_Date');
   h.definedata('_ret');
   h.definedone();
 end;

array x{100} _temporary_;
do until(last.PERMNO);
 set have;
 by PERMNO;
 _ret=ret;h.add();
end;

do until(last.PERMNO);
 set have;
 by PERMNO;
 n=0;call missing(of x{*});
 do i=intnx('month',monyy,-3) to intnx('month',monyy,-1,'e');
   if h.find(key:i)=0 then do;n+1;x{n}=_ret; end;
 end;
 std=std(of x{*});
 output;
end;

h.clear();
drop i _ret n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Mar 2018 13:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447089#M112248</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-20T13:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447218#M112286</link>
      <description>&lt;P&gt;It's lot faster now. It also shows the result i.e. the standard deviation. But it shows the following error many times:&lt;/P&gt;&lt;P&gt;ERROR: Duplicate key.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Mar 2018 18:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447218#M112286</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-20T18:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447388#M112376</link>
      <description>&lt;P&gt;That means there are multiply return for the same&amp;nbsp;&lt;SPAN&gt;PERMNO and NAMES_DATE .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Make sure a&amp;nbsp;PERMNO and a NAMES_DATE have &lt;STRONG&gt;only one return.&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To sum(return) in each&amp;nbsp;PERMNO and&amp;nbsp; NAMES_DATE combination.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;After that running code would not generate that error information.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc summary data=have;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by PERMNO&amp;nbsp;NAMES_DATE&amp;nbsp; ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;var return;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;output out=want sum=;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;OR you want the last return for each group of&amp;nbsp;PERMNO and&amp;nbsp; NAMES_DATE.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data want;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;set have;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by&amp;nbsp;PERMNO&amp;nbsp;NAMES_DATE&amp;nbsp; ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if last.NAMES_DATE&amp;nbsp;&amp;nbsp;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Mar 2018 12:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447388#M112376</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-21T12:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447394#M112379</link>
      <description>&lt;P&gt;OK. If you want retain those multiple returns in the same&amp;nbsp;PERMNO and the same Names_Date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile='c:\temp\daily_ret.txt' out=have dbms=tab replace;
guessingrows=32767;
run;
data have ;
 set have;
 ret=input(Returns,?? best32.);
 drop Returns;
 monyy=intnx('month',Names_Date,0);
 format monyy yymmn6.;
run;
proc sort data=have;by PERMNO monyy Names_Date;run;

data want;
 if _n_=1 then do;
   if 0 then set have(rename=(ret=_ret));
   declare hash h(multidata:'y');
   h.definekey('Names_Date');
   h.definedata('_ret');
   h.definedone();
 end;

array x{100} _temporary_;
do until(last.PERMNO);
 set have;
 by PERMNO;
 _ret=ret;h.add();
end;

do until(last.PERMNO);
 set have;
 by PERMNO;
 n=0;call missing(of x{*});
 do i=intnx('month',monyy,-3) to intnx('month',monyy,-1,'e');
   rc=h.find(key:i);
   do while(rc=0);
     n+1;x{n}=_ret;
     rc=h.find_next(key:i);
   end;
 end;
 std=std(of x{*});
 output;
end;

h.clear();
drop i _ret n rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Mar 2018 12:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447394#M112379</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-21T12:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447476#M112421</link>
      <description>&lt;P&gt;Thanks a lot Xia.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Mar 2018 16:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/447476#M112421</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-21T16:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450115#M113329</link>
      <description>&lt;P&gt;OK. If there was no ID column ,and want t-1 to t-12 rolling std.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
 if _n_=1 then do;
   if 0 then set have(rename=(ret=_ret));
   declare hash h(dataset:'have(rename=(ret=_ret))',multidata:'y');
   h.definekey('Names_Date');
   h.definedata('_ret');
   h.definedone();
 end;

array x{1000} _temporary_;
set have;

 n=0;call missing(of x{*});
 do i=intnx('month',monyy,-12) to intnx('month',monyy,-1,'e');
   rc=h.find(key:i);
   do while(rc=0);
     n+1;x{n}=_ret;
     rc=h.find_next(key:i);
   end;
 end;
 std=std(of x{*});

drop i _ret n rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 31 Mar 2018 10:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450115#M113329</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-31T10:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450126#M113333</link>
      <description>&lt;P&gt;This code shows the following error message:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Array subscript out of range at line 125 column 10.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This was&amp;nbsp;at line 125:&lt;/P&gt;&lt;P&gt;n+1;x{n}=_ret;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 31 Mar 2018 13:07:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450126#M113333</guid>
      <dc:creator>AbuChowdhury</dc:creator>
      <dc:date>2018-03-31T13:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450226#M113365</link>
      <description>&lt;P&gt;Sorry. This code is not for you . someone sent me a private message with this topic link.&lt;/P&gt;
&lt;P&gt;He or She want get the similar result with only one PERMNO . So I post it here. If that was not you ,ignore it .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile='c:\temp\daily_ret.txt' out=have dbms=tab replace;
guessingrows=32767;
run;
data have ;
 set have;
 ret=input(Returns,?? best32.);
 drop Returns;
 monyy=intnx('month',Names_Date,0);
 format monyy yymmn6.;
run;
proc sort data=have;by PERMNO monyy Names_Date;run;






data have;
 set have;
 if permno=10008;
run;


data want;
 if _n_=1 then do;
   if 0 then set have(rename=(ret=_ret));
   declare hash h(dataset:'have(rename=(ret=_ret))',multidata:'y');
   h.definekey('Names_Date');
   h.definedata('_ret');
   h.definedone();
 end;

array x{1000} _temporary_;
set have;

 n=0;call missing(of x{*});
 do i=intnx('month',monyy,-12) to intnx('month',monyy,-1,'e');
   rc=h.find(key:i);
   do while(rc=0);
     n+1;x{n}=_ret;
     rc=h.find_next(key:i);
   end;
 end;
 std=std(of x{*});

drop i _ret n rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Apr 2018 10:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/450226#M113365</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-01T10:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/488101#M127215</link>
      <description>&lt;P&gt;Hi Ksharp,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thank you very much for your answer. I am calculating the similar sigma ratio here, except that I require that if there are fewer than five nonzero observations over the 3 months used in the rolling window computation, SIGMA will be counted as missing and replace with the cross-sectional mean of SIGMA. May I ask how to add a line in your following code to implement ?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanks&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
create table want as
 select *,(select std(return) from have where id=a.id and date between intnx('month',a.date,-3) and a.date ) as std
   from have as a;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Aug 2018 05:46:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/488101#M127215</guid>
      <dc:creator>Nieves</dc:creator>
      <dc:date>2018-08-20T05:46:37Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation on rolling basis</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/488483#M127333</link>
      <description>&lt;P&gt;How do you calculate this cross-sectional mean of SIGMA ?&lt;/P&gt;
&lt;P&gt;And I suggest you start a new session ,this post is almost one year old .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following could give you a start.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
 select *,
case when count(return) &amp;lt;5 then .
else
(select std(return) from have where id=a.id and date between intnx('month',a.date,-3) and a.date )
end as std
   from have as a;
quit;

data final_want;
set want;
"replace missing with cross-sectional mean of SIGMA"
............
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 10:29:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-on-rolling-basis/m-p/488483#M127333</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-08-21T10:29:05Z</dc:date>
    </item>
  </channel>
</rss>

