<?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: counter that resets after 4 hours in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500007#M72578</link>
    <description>Do you have a license for SAS /ETS (time series)?</description>
    <pubDate>Fri, 28 Sep 2018 18:51:10 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-09-28T18:51:10Z</dc:date>
    <item>
      <title>counter that resets after 4 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500002#M72577</link>
      <description>&lt;P&gt;I am hoping to create a rolling counter but im struggling with a do loop.&amp;nbsp; The counter needs to count the number or records in a rolling 4 hour period by account.&amp;nbsp; I've included a sample of data and the expected counter output.&amp;nbsp; Appreciate the help&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 18:43:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500002#M72577</guid>
      <dc:creator>mac91red</dc:creator>
      <dc:date>2018-09-28T18:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: counter that resets after 4 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500007#M72578</link>
      <description>Do you have a license for SAS /ETS (time series)?</description>
      <pubDate>Fri, 28 Sep 2018 18:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500007#M72578</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-28T18:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: counter that resets after 4 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500031#M72579</link>
      <description>&lt;P&gt;Combine date and time variables into datetime variable called dt and run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=account dt counter);
array d {12}; /* Adjust limit to max number of obs per 4-hour window */
pos = 0;
do until(last.account);
    set have; by account;
    counter = 1;
    do i = 1 to dim(d);
        if dt - '04:00:00't &amp;gt;= d{i} then call missing(d{i});
        else counter = counter + 1;
        end;
    output;
    d{pos + 1} = dt;
    pos = mod(pos + 1, dim(d));
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Sep 2018 20:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500031#M72579</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-28T20:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: counter that resets after 4 hours</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500071#M72580</link>
      <description>&lt;P&gt;Like the &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;response, this uses an array to hold time values.&amp;nbsp; However, the array dimension should be set to the largest expected number of records for a single date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input account date :$6. time :time5.0 expected;
  format time time5.0;
datalines;
1 1-Aug  1:00 1
1 1-Aug  1:30 2
1 1-Aug  2:00 3
1 1-Aug  3:30 4
1 1-Aug  4:30 5
1 1-Aug  5:00 5
1 1-Aug  6:30 4
1 1-Aug  7:30 5
2 1-Aug 13:00 1
2 1-Aug 14:00 2
2 2-Aug 15:00 1
2 2-Aug 16:00 2
run;

data want (drop=_:);
  array tims {20} _temporary_;
  set have;
  by account date;

  if first.date then call missing(_n1,counter,of tims{*});
  _n1+1;
  tims{_n1}=time;
  counter+1;

  if first.date then _n2=1;
  do counter=counter by -1 while (tims{_n2} &amp;lt;= time-'04:00:00't);
	tims{_n2}=.;
	_n2+1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The program maintains&amp;nbsp;2 variables for indexing the time arrays:
&lt;OL&gt;
&lt;LI&gt;_n1, to point at the current time element&lt;/LI&gt;
&lt;LI&gt;_n2, to point at the earliest element having time within the 4-hour window.&amp;nbsp; So _N2 is always &amp;lt;= _N1&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;I believe your expected counter value for 7:30&amp;nbsp;&amp;nbsp; 1-Aug is in error.&amp;nbsp; You have counter=5, but that would require including the 3:30 record, exactly 4 hours earlier.&amp;nbsp; That would be inconsistent with your 5:00 record with counter=5, because it doesn't include the earlier 1:00 record.&amp;nbsp; &amp;nbsp; Now if you do want to include records exactly 4 hours old,&amp;nbsp;then change the&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#0000ff" face="Sasfont"&gt;while&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; (tims{_n2} &amp;lt;= time-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;'04:00:00't&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;)&lt;/FONT&gt;&lt;BR /&gt;to&amp;nbsp;&lt;BR /&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; while&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; (tims{_n2} &amp;lt; time-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;'04:00:00't&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 29 Sep 2018 04:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/counter-that-resets-after-4-hours/m-p/500071#M72580</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-29T04:20:36Z</dc:date>
    </item>
  </channel>
</rss>

