<?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: Regressions for rolling window for each company in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712014#M219422</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST2;
 WS = 5;
 NWIN = NRECS - WS +1;
 do W=1 TO NWIN;
   C=COMPANY;
   do P=W TO W + WS -1;
     set TEST point=P nobs=NRECS;
     if COMPANY = C then output;
     else leave;
   end;
 end;
 stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jan 2021 04:09:46 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-01-18T04:09:46Z</dc:date>
    <item>
      <title>Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712006#M219418</link>
      <description>&lt;P&gt;I would like to do regression for rolling windows of 5 years for a company. I used a brute force approach like the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 ws = 5;
 nwin = nrecs - ws +1;
 do w=1 to nwin;
 do p=w to w + ws -1;
 set test point=p nobs=nrecs;
 output;
 end;
 end;
 stop;
run;
data lib.test; set test; run;

proc reg data=test noprint outest=myests;
 by w;
 model y=x;
quit;
data lib.myests; set myests; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, when I checked my data, I realized that if a cluster/group does not have enough observations of 5, it takes an observation of another company to make it 5. For example:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="image (1).png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53590i6B2464EC339CADD8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="image (1).png" alt="image (1).png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The group w = 1 is fine. However, in the group w = 2, when A &amp;amp; A Foods LTD has only 4 observations, it takes an observation (the first one) of AAON Inc. I would like something like, if a cluster/group has fewer than 5 observations, then it should be removed (not considered), like the case of w = 2 above. How may I do this?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 02:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712006#M219418</guid>
      <dc:creator>kain</dc:creator>
      <dc:date>2021-01-18T02:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712014#M219422</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TEST2;
 WS = 5;
 NWIN = NRECS - WS +1;
 do W=1 TO NWIN;
   C=COMPANY;
   do P=W TO W + WS -1;
     set TEST point=P nobs=NRECS;
     if COMPANY = C then output;
     else leave;
   end;
 end;
 stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 04:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712014#M219422</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-18T04:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712018#M219425</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; I would like something like, if a cluster/group has fewer than 5 observations, then it should be removed (not considered),&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Actually, my code only removes the rogue company, not the whole group.&lt;/P&gt;
&lt;P&gt;You need to iterate twice to remove small groups: once to count, and once again to output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do the first 2 records look identical?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jan 2021 04:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712018#M219425</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-18T04:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712021#M219427</link>
      <description>&lt;P&gt;If you have no instance of interior "holes" in a permno series, but sometimes have short series, then this will work.&amp;nbsp; And it will be &lt;EM&gt;&lt;STRONG&gt;far&lt;/STRONG&gt;&lt;/EM&gt; more efficient than using POINT= in a set statement, which will read each obs 5 times.&amp;nbsp; Below each obs is read once, stored in an array, and subsequently output five times, once fpr each window that contains it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, you say (emphasis mine):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358593"&gt;@kain&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to do regression for rolling windows of &lt;EM&gt;&lt;STRONG&gt;5 years for a company&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;but your regression, if the data were correctly created. would do one regression for ALL companies belonging to each five-year window.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which is it?&amp;nbsp; regression for EACH company in a window&amp;nbsp; (i.e. Nwindows * Ncompany regressions), or is it ALL companies in each window (Nwindows regressions)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below creates the data you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data windows / view=windows;
  do until (last.permno);
    set test;
    by permno;

    /* Make an array for each variable in the regression  */
    /* Make lower and upper bounds to span all your series */
    array y_history {2001:2020} ;
    array x_history {2001:2020} ;

    if first.permno then min_year=year(date);
    y_history{year(date)}=y;
    x_history{year(date)}=x;
  end;

  max_year=year(date);

  if max_year&amp;gt;=min_year+4 then do w=min_year to max_year-4;
    do year=w to w+4;
      y=y_history{year};
      x=x_history{year};
      output;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if it's one regression per window per company, then just use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc reg data=windows;
  by permno w;
  model y=x;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if it's one regression covering ALL companies in a window, then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=windows out=sorted_data;
  by w permno;
run;
proc reg data=sorted_data;
  by w;
  model y=x;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 19:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/712021#M219427</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-07T19:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931095#M366317</link>
      <description>&lt;P&gt;Dear Kain,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you are doing well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please reply to me if this problem has been solved?&lt;/P&gt;&lt;P&gt;I have the same issue here and I try many time, the different companies will always interact with each other in the last observations for the former one. This issue is killing me &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2024 09:49:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931095#M366317</guid>
      <dc:creator>Whitepepper</dc:creator>
      <dc:date>2024-06-06T09:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931101#M366320</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466452"&gt;@Whitepepper&lt;/a&gt;&amp;nbsp;this has been answered in your &lt;A href="https://communities.sas.com/t5/SAS-Programming/Rolling-window-regression-for-one-company/m-p/931097#M366318" target="_self"&gt;other thread&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2024 11:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931101#M366320</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-06T11:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: Regressions for rolling window for each company</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931109#M366326</link>
      <description>&lt;P&gt;Also,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466452"&gt;@Whitepepper&lt;/a&gt;&amp;nbsp;this current thread has a solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2024 11:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regressions-for-rolling-window-for-each-company/m-p/931109#M366326</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-06T11:31:33Z</dc:date>
    </item>
  </channel>
</rss>

