<?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/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>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-03-18T20:15:37Z</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>

