<?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: how to estimate rolling window beta in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362048#M85487</link>
    <description>&lt;P&gt;OK, so now it is clear you want&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Every Rolling window whose report_date is &amp;gt;= first_date+47 months&lt;/LI&gt;
&lt;LI&gt;Any size&amp;nbsp;window&amp;nbsp;up to 48 months&amp;nbsp; (no minimum size?)&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then I'd suggest not bothering with arrays of size 48, with all the attendant MOD function usage.&amp;nbsp; Instead have an array of size 192 (12 months for years 2001-2016).&amp;nbsp; This will allow&amp;nbsp;direct indexing of the arrays&amp;nbsp;on MONTHNUM (= 1 + n of months since firstdate).&amp;nbsp; Easier to store in the array and easier to extract.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let time_range=%eval(12*(1+2016-2001));
%put &amp;amp;=time_range;

data nstockregfinal5  / view= nstockregfinal5;
  array _X1 {&amp;amp;time_range} _temporary_ ;
  array _X2 {&amp;amp;time_range} _temporary_ ;
  array _Y {&amp;amp;time_range} _temporary_ ;

  set price.nstockregfinal3;
  by  CUSIP;

  retain first_date;
  if first.cusip then do;
    call missing(of _x1{*},of _x2{*},of _y{*});
    first_date=report_date;
  end;

  monthnum=1+intck('month',first_date,report_date);

  _X1{monthnum}=Rtm;
  _X2{monthnum}=Rti;
  _Y{monthnum}=Rt;

  if monthnum&amp;gt;=48 then do m=monthnum-47 to monthnum;
    if _Y{m}=. then continue;
    Rtm=_X1{m};   /*editted correction - use M instead of I for index*/
    Rti=_X2{m};
    Rt=_Y{m};
    output;
  end;
run;

proc reg data= nstockregfinal5  noprint outest=myests;
  by CUSIP REPORT_DATE;
  model Rt = Rtm Rti;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the&amp;nbsp;"if _Y{M}=. then continue" statement says to skip the M'th iteration of the loop if the M'th observation of _Y is missing.&amp;nbsp; That's the way to prevent outputting missing months.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 28 May 2017 01:48:50 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-05-28T01:48:50Z</dc:date>
    <item>
      <title>how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361376#M85207</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I need your help please because I am having some troubles with the sas script to estimate rolling window betas .&lt;/P&gt;&lt;P&gt;I want to estimate for each cusip code &amp;nbsp;in each month , the &amp;nbsp;betai and betam using the previous four years of monthly returns data:&lt;/P&gt;&lt;P&gt;Rt = alpha + betai Rti &amp;nbsp;+ betam Rtm +&amp;nbsp;&amp;nbsp;e&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have 20000 stocks in my dataset for a period of 2001-2016. &amp;nbsp;The starting period of my study is 01/01/2005 so I would like to &amp;nbsp;estimate the betas using the previous 48 months date. &amp;nbsp;The problem is that not all the stocks have previous complete 48 month data returns. I have trying the following code but it doesn't work well. I would like to know your suggestions please. &amp;nbsp;my dataset that looks like that:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;CUSIP Rt &amp;nbsp;Rtm Rti Report_date&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;and I would like to obtain&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;REPORT_DATE CUSIP betai betam&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Thank you in advance&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data nstockregfinal5  / view= nstockregfinal5;
 array _X1 {48} _temporary_ ;
 array _X2 {48} _temporary_ ;
 array _Y {48} _temporary_ ;
 set price.nstockregfinal3;
 by  CUSIP;
 retain N 0;
 N = ifn(first.CUSIP,1,N+1);
 I=mod(N-1,48)+1;
 _X1{I}=Rtm;
 _X2{I}=Rti;
 _Y{I}=Rt;
 if N&amp;gt;=48 then do I= 1 to 48;
 Rtm=_X1{I};
 Rti=_X2{I};
 Rt=_Y{I};
 output;
 end;
run;
proc reg data= nstockregfinal5  noprint outest=myests;
 by CUSIP REPORT_DATE;
 model Rt = Rtm Rti;
 run;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 May 2017 20:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361376#M85207</guid>
      <dc:creator>bera00</dc:creator>
      <dc:date>2017-05-24T20:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361847#M85397</link>
      <description>&lt;P&gt;You need to check whether the most-recent 48 records comprise a complete 48-month window.&amp;nbsp; You can use the lag function for this purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of incrementing N by 1 for each incoming record, calculate &lt;EM&gt;&lt;STRONG&gt;MONTHNUM=1+number of months since first report_date&lt;/STRONG&gt;&lt;/EM&gt; for the given CUSIP.&amp;nbsp;&amp;nbsp;Whereas the series N&amp;nbsp;values would never have&amp;nbsp;"holes", the MONTHNUM series would have holes in the case of a missing month.&amp;nbsp;&amp;nbsp;&amp;nbsp;You can still&amp;nbsp;calculate I=mod(monthnum-1,48)+1 to specify the array element.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But now, only if current monthnum=47+lag47(monthnum) will you have a complete 48-month windows.&amp;nbsp; And of course lag47(cusip)=cusip to make sure you are not combining series from two cusips.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I modified your program to use this test.&amp;nbsp; The code &lt;EM&gt;in italics&lt;/EM&gt; is new.&amp;nbsp; The code &lt;STRIKE&gt;in strikeout font&lt;/STRIKE&gt; is your code that should be removed in this version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; nstockregfinal5 / &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;view&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;= nstockregfinal5;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; array&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; _X1 {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;} &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;_temporary_&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; array&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; _X2 {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;} &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;_temporary_&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; array&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; _Y {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;} &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;_temporary_&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; set&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; price.nstockregfinal3;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; by&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; CUSIP;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; &lt;STRIKE&gt;retain&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;STRIKE&gt;&lt;FONT face="Sasfont"&gt; N &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;&lt;STRIKE&gt;N = ifn(first.CUSIP,&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;STRIKE&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;,N+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;);&lt;/FONT&gt;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;&lt;STRIKE&gt;I=mod(N-&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;STRIKE&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;)+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; retain&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; first_date;&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; if&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; first.cusip &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;then&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; first_date=report_date;&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;monthnum=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;+intck(&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'month'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;,first_date,report_date);&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;I=mod(monthnum-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;)+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; _X1{I}=Rtm;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; _X2{I}=Rti;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; _Y{I}=Rt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt;&amp;nbsp; &lt;EM&gt;OKwindow = (lag47(month)+&lt;/EM&gt;&lt;/FONT&gt;&lt;EM&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;47&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;=month) and (lag47(cusip)=cusip);&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; &lt;STRIKE&gt;if&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;STRIKE&gt;&lt;FONT face="Sasfont"&gt; N&amp;gt;=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;do&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; I= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; &lt;EM&gt;if&lt;/EM&gt;&lt;/FONT&gt;&lt;EM&gt;&lt;FONT face="Sasfont"&gt; OKwindow &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;do&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; I=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;48&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;Rtm=_x1{I};&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rti=_x2{I};&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rt=_Y{I};&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; end&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Sasfont"&gt;reg&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;data&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;= nstockregfinal5 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;noprint&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Sasfont"&gt;outest&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;=myests;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; by&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; CUSIP REPORT_DATE;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; model&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; Rt = Rtm Rti;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&amp;nbsp; run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Sasfont"&gt;quit;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2017 03:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361847#M85397</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-26T03:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361892#M85426</link>
      <description>&lt;P&gt;Thank you so much for your answer. &amp;nbsp;If i undestood well, this code &amp;nbsp;keeps only the stocks that have previous 48 month records?&lt;/P&gt;&lt;P&gt;However in my study I need to keep all the stocks and for those that don't have 48 then take the available records.. is that possible?&lt;/P&gt;&lt;P&gt;I have been trying this script but it works only for one cusip not for many and it doesn t give me what I want : beta coefficients for each cusip at each date.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data nstockregfinal4 / view= nstockregfinal4;
do grp = 0 to nrecs-48;
do j = 1 + grp to 48 + grp;
set nstockregfinal3 nobs=nrecs point=j;
output;
end;
end;
stop;
run;

proc reg data=nstockregfinal4 outest=stats noprint;
by grp;
model Rt = Rtm Rti;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 May 2017 07:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/361892#M85426</guid>
      <dc:creator>bera00</dc:creator>
      <dc:date>2017-05-26T07:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362048#M85487</link>
      <description>&lt;P&gt;OK, so now it is clear you want&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Every Rolling window whose report_date is &amp;gt;= first_date+47 months&lt;/LI&gt;
&lt;LI&gt;Any size&amp;nbsp;window&amp;nbsp;up to 48 months&amp;nbsp; (no minimum size?)&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then I'd suggest not bothering with arrays of size 48, with all the attendant MOD function usage.&amp;nbsp; Instead have an array of size 192 (12 months for years 2001-2016).&amp;nbsp; This will allow&amp;nbsp;direct indexing of the arrays&amp;nbsp;on MONTHNUM (= 1 + n of months since firstdate).&amp;nbsp; Easier to store in the array and easier to extract.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let time_range=%eval(12*(1+2016-2001));
%put &amp;amp;=time_range;

data nstockregfinal5  / view= nstockregfinal5;
  array _X1 {&amp;amp;time_range} _temporary_ ;
  array _X2 {&amp;amp;time_range} _temporary_ ;
  array _Y {&amp;amp;time_range} _temporary_ ;

  set price.nstockregfinal3;
  by  CUSIP;

  retain first_date;
  if first.cusip then do;
    call missing(of _x1{*},of _x2{*},of _y{*});
    first_date=report_date;
  end;

  monthnum=1+intck('month',first_date,report_date);

  _X1{monthnum}=Rtm;
  _X2{monthnum}=Rti;
  _Y{monthnum}=Rt;

  if monthnum&amp;gt;=48 then do m=monthnum-47 to monthnum;
    if _Y{m}=. then continue;
    Rtm=_X1{m};   /*editted correction - use M instead of I for index*/
    Rti=_X2{m};
    Rt=_Y{m};
    output;
  end;
run;

proc reg data= nstockregfinal5  noprint outest=myests;
  by CUSIP REPORT_DATE;
  model Rt = Rtm Rti;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the&amp;nbsp;"if _Y{M}=. then continue" statement says to skip the M'th iteration of the loop if the M'th observation of _Y is missing.&amp;nbsp; That's the way to prevent outputting missing months.&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2017 01:48:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362048#M85487</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-28T01:48:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362159#M85523</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;thank you again for your answer . If I would like to set a minimum of obs then how can I do it? In addition&lt;/P&gt;&lt;P&gt;I am getting in the log window :&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;NOTE: Variable I is uninitialized.&lt;BR /&gt;ERROR: Array subscript out of range at line 46 column 9.&lt;BR /&gt;Cusip=000360206 Rt=10.433725466 Rtm=-3.146277904 Rti=-1.741508366 REPORT_DATE=04/30/2005 FIRST.Cusip=0 LAST.Cusip=0&lt;BR /&gt;first_date=15126 monthnum=48 m=1 I=. _ERROR_=1 _N_=68&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sat, 27 May 2017 06:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362159#M85523</guid>
      <dc:creator>bera00</dc:creator>
      <dc:date>2017-05-27T06:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to estimate rolling window beta</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362253#M85572</link>
      <description>&lt;P&gt;As to the uniintialized variable I, that's becuase I used I as the index of arrays in the second loop, even though the loop index was M.&amp;nbsp; Replace the {I} occurence with {M}.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So now you want a minumum window size criterion.&amp;nbsp; In that case I suggest you add anew array: _SEQ_N{}, of size 192 in your case.&amp;nbsp; For each month index M, it will have the "rank" of the corresponding data record.&amp;nbsp; For instance if your first 4 observations are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1/2001 with Y=101&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2/2001&amp;nbsp; y=101.5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4/2001&amp;nbsp; y=99.3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5/2000&amp;nbsp; y=99.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then _Y{1}=101, _Y{2}=101.5, _Y{3}=., _Y{4}=99.3, and _Y{5}=99.4.&amp;nbsp;&amp;nbsp; Note _Y{3} is a missing value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the new array will have values&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; _SEQ_N{1}=1&amp;nbsp;&amp;nbsp; _SEQ_N{2}=2&amp;nbsp;&amp;nbsp; _SEQ_N{3}=.&amp;nbsp;&amp;nbsp; _SEQ_N{4}=3&amp;nbsp;&amp;nbsp; _SEQ_N{5}=4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just as _Y{3}=., so does _SEQ_N{3}.&amp;nbsp;&amp;nbsp; BUT ... _SEQ_N{4}=3, because it is the 3rd non-missing value in the series.&amp;nbsp; Later on when you get to months&amp;nbsp;48, 49, 50, 51, etc. you can just subtract the pertinent _SEQ_N values to get the number of non-missing months available in the 48-month range.&amp;nbsp; Here's the modified program.&amp;nbsp; Note the new macrovar MIN_MCOUNT, which provides the minimum acceptable count of months in your window (36 in this example):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let time_range=%eval(12*(1+2016-2001));
%put &amp;amp;=time_range;
%let min_mcount=36;

data nstockregfinal5  / view= nstockregfinal5;
  array _X1 {&amp;amp;time_range} _temporary_ ;
  array _X2 {&amp;amp;time_range} _temporary_ ;
  array _Y {&amp;amp;time_range} _temporary_ ;
  array _seq_N {&amp;amp;time_range} _temporary_;

  set price.nstockregfinal3;
  by  CUSIP;

  retain first_date ;
  if first.cusip then do;
    call missing(of _x1{*},of _x2{*},of _y{*},of _seq_N{*});
    first_date=report_date;
    _seq=0;
  end;

  monthnum=1+intck('month',first_date,report_date);
  _seq+1;  /*Increment valid record count */

  _X1{monthnum}=Rtm;
  _X2{monthnum}=Rti;
  _Y{monthnum}=Rt;
  _seq_N{monthnum}=_seq;

  _mcount=.;   /* Initialize valid count of months for this window*/
  if monthnum&amp;gt;=48 then do;
    /* Find the earliest non-missing _Y */
    do m=monthnum-47 to monthnum while (_Y{m}=.);
    end;
    /* Now get count of non-missing months in this 48-month window*/
    _mcount=1+_seq_N{monthnum}-_seq_N{m};

    if _mcount&amp;gt;= &amp;amp;min_mcount then do m=monthnum-47 to monthnum;
      if _Y{m}=. then continue;
      Rtm=_X1{m}; 
      Rti=_X2{m};
      Rt=_Y{m};
      output;
    end;
  end;
run;

proc reg data= nstockregfinal5  noprint outest=myests;
  by CUSIP REPORT_DATE;
  model Rt = Rtm Rti;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2017 02:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-estimate-rolling-window-beta/m-p/362253#M85572</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-28T02:22:10Z</dc:date>
    </item>
  </channel>
</rss>

