<?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 How do I run a  do-loop by groups? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677917#M79388</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found some sas code online that calculates a moving average.&amp;nbsp; I don't have SAS/ETS.&amp;nbsp; It works well, but I have some new data that is structured vertically. It consists of five variables, Date, Zipcode, PCT, COUNT, and TOTAL.&amp;nbsp; I want to run the code through each group of zipcodes, calculating the new moving average for each.&amp;nbsp; Once it encounters 60602, a new moving average is calculated. I have a feeling it's something simple but I can't think of it.&amp;nbsp; Thank you.&lt;/P&gt;&lt;PRE&gt;data zips1;
  infile datalines dsd truncover;
  input Date:DATE9. zipcode:32. pct:32. count:32. total:32.;
  format Date DATE9.;
  datalines;
01JAN2020 60601 16.666667 1 6
02JAN2020 60601 0 0 8
03JAN2020 60601 14.285714 1 7
04JAN2020 60601 0 0 5
05JAN2020 60601 0 0 7
06JAN2020 60601 0 0 8
07JAN2020 60601 0 0 6
08JAN2020 60601 0 0 8
09JAN2020 60601 20 1 5
10JAN2020 60601 0 0 6
11JAN2020 60601 0 0 8
12JAN2020 60601 0 0 4
13JAN2020 60601 0 0 8
14JAN2020 60601 0 0 10
15JAN2020 60601 0 0 9
16JAN2020 60601 25 1 4
17JAN2020 60601 0 0 4
18JAN2020 60601 0 0 6
19JAN2020 60601 0 0 4
20JAN2020 60601 0 0 6

data zips2 ; keep date zipcode pct count total n meanxi sumxi;
set zips1;
 if missing(count ) then
 do;
 OBS = 0;
 count = 0.0;
 end;
 else OBS = 1;
 XI7 = lag7(count );
 OBS7 = lag7(obs);
 if missing(xi7) then xi7 = 0.0;
 if missing(obs7) then obs7 = 0;
 LDATE = lag2(date);
 format ldate date9. ; 

 if _N_ = 1 then
 do;
 SUMXI = 0.0;
 N = 0;
 end;
 else;
 sumxi = sumxi + count - xi7;
 n = n + obs - obs7;
 MEANXI = sumxi / n ;
 retain sumxi n;
run;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Aug 2020 22:59:25 GMT</pubDate>
    <dc:creator>eramirez</dc:creator>
    <dc:date>2020-08-19T22:59:25Z</dc:date>
    <item>
      <title>How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677917#M79388</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found some sas code online that calculates a moving average.&amp;nbsp; I don't have SAS/ETS.&amp;nbsp; It works well, but I have some new data that is structured vertically. It consists of five variables, Date, Zipcode, PCT, COUNT, and TOTAL.&amp;nbsp; I want to run the code through each group of zipcodes, calculating the new moving average for each.&amp;nbsp; Once it encounters 60602, a new moving average is calculated. I have a feeling it's something simple but I can't think of it.&amp;nbsp; Thank you.&lt;/P&gt;&lt;PRE&gt;data zips1;
  infile datalines dsd truncover;
  input Date:DATE9. zipcode:32. pct:32. count:32. total:32.;
  format Date DATE9.;
  datalines;
01JAN2020 60601 16.666667 1 6
02JAN2020 60601 0 0 8
03JAN2020 60601 14.285714 1 7
04JAN2020 60601 0 0 5
05JAN2020 60601 0 0 7
06JAN2020 60601 0 0 8
07JAN2020 60601 0 0 6
08JAN2020 60601 0 0 8
09JAN2020 60601 20 1 5
10JAN2020 60601 0 0 6
11JAN2020 60601 0 0 8
12JAN2020 60601 0 0 4
13JAN2020 60601 0 0 8
14JAN2020 60601 0 0 10
15JAN2020 60601 0 0 9
16JAN2020 60601 25 1 4
17JAN2020 60601 0 0 4
18JAN2020 60601 0 0 6
19JAN2020 60601 0 0 4
20JAN2020 60601 0 0 6

data zips2 ; keep date zipcode pct count total n meanxi sumxi;
set zips1;
 if missing(count ) then
 do;
 OBS = 0;
 count = 0.0;
 end;
 else OBS = 1;
 XI7 = lag7(count );
 OBS7 = lag7(obs);
 if missing(xi7) then xi7 = 0.0;
 if missing(obs7) then obs7 = 0;
 LDATE = lag2(date);
 format ldate date9. ; 

 if _N_ = 1 then
 do;
 SUMXI = 0.0;
 N = 0;
 end;
 else;
 sumxi = sumxi + count - xi7;
 n = n + obs - obs7;
 MEANXI = sumxi / n ;
 retain sumxi n;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Aug 2020 22:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677917#M79388</guid>
      <dc:creator>eramirez</dc:creator>
      <dc:date>2020-08-19T22:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677921#M79389</link>
      <description>&lt;P&gt;What do you want the output to look like?&amp;nbsp; Since you are doing 7-day rolling statistics, do you want to start each zip code with the 7th observation, such that it is the first with a completely populated 7-day window?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677921#M79389</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-19T23:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677923#M79390</link>
      <description>&lt;P&gt;Here’s a quick example.&amp;nbsp;&lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/27e23c015eae7953eff2" target="_blank"&gt;https://gist.github.com/statgeek/27e23c015eae7953eff2&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change the min/max to mean/median or whatever stat you’re calculating and of course the array lengths as needed.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32837"&gt;@eramirez&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I found some sas code online that calculates a moving average.&amp;nbsp; I don't have SAS/ETS.&amp;nbsp; It works well, but I have some new data that is structured vertically. It consists of five variables, Date, Zipcode, PCT, COUNT, and TOTAL.&amp;nbsp; I want to run the code through each group of zipcodes, calculating the new moving average for each.&amp;nbsp; Once it encounters 60602, a new moving average is calculated. I have a feeling it's something simple but I can't think of it.&amp;nbsp; Thank you.&lt;/P&gt;
&lt;PRE&gt;data zips1;
  infile datalines dsd truncover;
  input Date:DATE9. zipcode:32. pct:32. count:32. total:32.;
  format Date DATE9.;
  datalines;
01JAN2020 60601 16.666667 1 6
02JAN2020 60601 0 0 8
03JAN2020 60601 14.285714 1 7
04JAN2020 60601 0 0 5
05JAN2020 60601 0 0 7
06JAN2020 60601 0 0 8
07JAN2020 60601 0 0 6
08JAN2020 60601 0 0 8
09JAN2020 60601 20 1 5
10JAN2020 60601 0 0 6
11JAN2020 60601 0 0 8
12JAN2020 60601 0 0 4
13JAN2020 60601 0 0 8
14JAN2020 60601 0 0 10
15JAN2020 60601 0 0 9
16JAN2020 60601 25 1 4
17JAN2020 60601 0 0 4
18JAN2020 60601 0 0 6
19JAN2020 60601 0 0 4
20JAN2020 60601 0 0 6

data zips2 ; keep date zipcode pct count total n meanxi sumxi;
set zips1;
 if missing(count ) then
 do;
 OBS = 0;
 count = 0.0;
 end;
 else OBS = 1;
 XI7 = lag7(count );
 OBS7 = lag7(obs);
 if missing(xi7) then xi7 = 0.0;
 if missing(obs7) then obs7 = 0;
 LDATE = lag2(date);
 format ldate date9. ; 

 if _N_ = 1 then
 do;
 SUMXI = 0.0;
 N = 0;
 end;
 else;
 sumxi = sumxi + count - xi7;
 n = n + obs - obs7;
 MEANXI = sumxi / n ;
 retain sumxi n;
run;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Aug 2020 23:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/677923#M79390</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-19T23:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678096#M79393</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for the reply.&amp;nbsp; The results can start with the 7th observation if that helps, the variable N will indicate when the 7th observation begins so when I overlay&amp;nbsp;the MEANXI (line) values over the count (bar), I can use N&amp;gt;6 to exclude those first six values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Enrique&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 13:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678096#M79393</guid>
      <dc:creator>eramirez</dc:creator>
      <dc:date>2020-08-20T13:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678248#M79410</link>
      <description>&lt;P&gt;In cases like this, I would suggest maintaining an array containing the most recent 7 values of the variables in question.&amp;nbsp; Here's an example getting the 7-day rolling mean of COUNT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data zips1;&lt;BR /&gt;infile datalines truncover;&lt;BR /&gt;input Date:DATE9. zipcode:32. pct:32. count:32. total:32.;&lt;BR /&gt;format Date DATE9.;&lt;BR /&gt;datalines;&lt;BR /&gt;01JAN2020 60601 16.666667 1 6&lt;BR /&gt;02JAN2020 60601 0 0 8&lt;BR /&gt;03JAN2020 60601 14.285714 1 7&lt;BR /&gt;04JAN2020 60601 0 0 5&lt;BR /&gt;05JAN2020 60601 0 0 7&lt;BR /&gt;06JAN2020 60601 0 0 8&lt;BR /&gt;07JAN2020 60601 0 0 6&lt;BR /&gt;08JAN2020 60601 0 0 8&lt;BR /&gt;09JAN2020 60601 20 1 5&lt;BR /&gt;10JAN2020 60601 0 0 6&lt;BR /&gt;11JAN2020 60601 0 0 8&lt;BR /&gt;12JAN2020 60601 0 0 4&lt;BR /&gt;13JAN2020 60601 0 0 8&lt;BR /&gt;14JAN2020 60601 0 0 10&lt;BR /&gt;15JAN2020 60601 0 0 9&lt;BR /&gt;16JAN2020 60601 25 1 4&lt;BR /&gt;17JAN2020 60601 0 0 4&lt;BR /&gt;18JAN2020 60601 0 0 6&lt;BR /&gt;19JAN2020 60601 0 0 4&lt;BR /&gt;20JAN2020 60601 0 0 6&lt;BR /&gt;run;&lt;BR /&gt;data zips2;&lt;BR /&gt;  set zips1;&lt;BR /&gt;  by zipcode;&lt;BR /&gt;  obs+1;&lt;BR /&gt;  if first.zipcode then obs=1;&lt;BR /&gt;  array _cntarray {0:6} _temporary_;&lt;BR /&gt;  _cntarray{mod(obs,7)}=count;&lt;BR /&gt;  if obs&amp;gt;=7;&lt;BR /&gt;  mean_count=mean(of _cntarray{*});&lt;BR /&gt;run;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, if you really want to maintain a rolling sum to which you add the current COUNT and subtract lag7(count), followed by division-by-7, you could do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zips2;
  set zips1;
  by zipcode date ;
  obs+1;
  if first.zipcode then obs=1;
  sum_count + count + -coalesce(lag7(count),0);
  if obs&amp;gt;=7;
  mean_count=sum_count/7;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem with the second approach is that, for long time series, you could accumulate some minor computational rounding errors, such that the end of the series might not excactly equal one-seventh of the sum of the last 7 obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OTOH, the second approach could be a bit faster, especially if you want, say, a 40-day rolling window instead of a 7-day window.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 19:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678248#M79410</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-20T19:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678273#M79413</link>
      <description>&lt;P&gt;Thank you so much! The first approach works great.&amp;nbsp; I potentially may have long time series data so thank you for the 2nd method.&amp;nbsp; I will keep them both handy.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 21:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678273#M79413</guid>
      <dc:creator>eramirez</dc:creator>
      <dc:date>2020-08-20T21:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I run a  do-loop by groups?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678274#M79414</link>
      <description>&lt;P&gt;Thank you, this works also and useful for other stats!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 21:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-run-a-do-loop-by-groups/m-p/678274#M79414</guid>
      <dc:creator>eramirez</dc:creator>
      <dc:date>2020-08-20T21:29:27Z</dc:date>
    </item>
  </channel>
</rss>

