<?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 Find Max Count Grouped by Time Period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-Max-Count-Grouped-by-Time-Period/m-p/778071#M247650</link>
    <description>&lt;P&gt;Suppose I have a dataset as follows with two variables, one group_id variable and a date variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data id_dates;
input group_id $ date :mmddyy10.;
format date mmddyy10.;
datalines;
2A 01/04/2020 
2A 02/04/2020
2A 05/10/2020
2A 01/04/2020 
2A 04/07/2020 
2A 04/10/2020
2A 04/10/2020 
2A 04/10/2020 
2A 04/16/2020
2B 12/19/2018
2B 12/19/2018
2B 12/19/2018 
2B 01/23/2019
2B 12/18/2018 
2B 12/23/2018
2B 12/19/2018 
2B 08/05/2018
2B 11/19/2018 
2B 01/01/2019
;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the max number of records within any 1 day period, 2 day period, and any 7 day period, given the dates are from within 60 days from the latest date, grouped by the group_id. My output from the example dataset should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;group_id 1_day_max 2_day_max 1_week_max max_date
2A       3         3         4          05/10/2020
2B       4         5         6          01/24/2019&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I can get the "max_date" appended, but I'm having trouble with the 2 day/1 week max variables. For finding the 1 day max, I did it in a few steps I believe but there might be a cleaner way if anyone has input or notices any issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;proc sql;
create table max_dates as select group_id, max(date) format mmddyy10. as max_date from id_dates group by group_id;
create table by_date as select a.group_id,count(a.date) as num from id_dates a join max_dates b on a.group_id=b.group_id where date &amp;gt; intnx('day', max_date, -60)  group by a.group_id, date;
create table max as select group_id,max(num) as max_num from by_date group by group_id;
quit;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Nov 2021 15:44:40 GMT</pubDate>
    <dc:creator>sashelppls</dc:creator>
    <dc:date>2021-11-03T15:44:40Z</dc:date>
    <item>
      <title>Find Max Count Grouped by Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Max-Count-Grouped-by-Time-Period/m-p/778071#M247650</link>
      <description>&lt;P&gt;Suppose I have a dataset as follows with two variables, one group_id variable and a date variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data id_dates;
input group_id $ date :mmddyy10.;
format date mmddyy10.;
datalines;
2A 01/04/2020 
2A 02/04/2020
2A 05/10/2020
2A 01/04/2020 
2A 04/07/2020 
2A 04/10/2020
2A 04/10/2020 
2A 04/10/2020 
2A 04/16/2020
2B 12/19/2018
2B 12/19/2018
2B 12/19/2018 
2B 01/23/2019
2B 12/18/2018 
2B 12/23/2018
2B 12/19/2018 
2B 08/05/2018
2B 11/19/2018 
2B 01/01/2019
;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the max number of records within any 1 day period, 2 day period, and any 7 day period, given the dates are from within 60 days from the latest date, grouped by the group_id. My output from the example dataset should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;group_id 1_day_max 2_day_max 1_week_max max_date
2A       3         3         4          05/10/2020
2B       4         5         6          01/24/2019&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I can get the "max_date" appended, but I'm having trouble with the 2 day/1 week max variables. For finding the 1 day max, I did it in a few steps I believe but there might be a cleaner way if anyone has input or notices any issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;proc sql;
create table max_dates as select group_id, max(date) format mmddyy10. as max_date from id_dates group by group_id;
create table by_date as select a.group_id,count(a.date) as num from id_dates a join max_dates b on a.group_id=b.group_id where date &amp;gt; intnx('day', max_date, -60)  group by a.group_id, date;
create table max as select group_id,max(num) as max_num from by_date group by group_id;
quit;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 15:44:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Max-Count-Grouped-by-Time-Period/m-p/778071#M247650</guid>
      <dc:creator>sashelppls</dc:creator>
      <dc:date>2021-11-03T15:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find Max Count Grouped by Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Max-Count-Grouped-by-Time-Period/m-p/778166#M247695</link>
      <description>&lt;P&gt;Below what I could come-up with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input group_id $ date :mmddyy10.;
  format date date9.;
  datalines;
2A 01/04/2020 
2A 02/04/2020
2A 05/10/2020
2A 01/04/2020 
2A 04/07/2020 
2A 04/10/2020
2A 04/10/2020 
2A 04/10/2020 
2A 04/16/2020
2B 12/19/2018
2B 12/19/2018
2B 12/19/2018 
2B 01/23/2019
2B 12/18/2018 
2B 12/23/2018
2B 12/19/2018 
2B 08/05/2018
2B 11/19/2018 
2B 01/01/2019
;

proc sql;
  create table inter as
  select 
    l.group_id,
    l.date,
    r.max_date,
    l.cnt_1day
  from
  (
    select 
      group_id,
      date,
      count(*) as cnt_1day
    from have
    group by group_id, date
  ) l
  inner join
  (
    select 
      group_id,
      max(date) as max_date format=date9.
    from have
    group by group_id
  ) r
  on 
  l.group_id=r.group_id and r.max_date-l.date between 0 and 60
  order by group_id, date
  ;
quit;

data want;
  set inter;
  by group_id date;
  
  array _dt_  {7} 8 _temporary_;
  array _cnt_ {7} 8 _temporary_;

/*  array _dt_  {7} 8 ;*/
/*  array _cnt_ {7} 8 ;*/
/*  retain _dt_ _cnt_;*/

  if first.group_id then call missing(of _dt_[*], of _cnt_[*]);

  _k=mod(_n_,7)+1;
  _dt_[_k]  =date;
  _cnt_[_k] =cnt_1day;

  do _i=1 to dim(_dt_);
    if 0&amp;lt;=date-_dt_[_i]&amp;lt;=1 then cnt_2day+_cnt_[_i];
    if 0&amp;lt;=date-_dt_[_i]&amp;lt;=6 then cnt_7day+_cnt_[_i];
  end;

  output;
  call missing(of cnt_:);
  drop _k _i;

run;

proc sql;
  select
    group_id,
    max(cnt_1day) as cnt_1day,
    max(cnt_2day) as cnt_2day,
    max(cnt_7day) as cnt_7day 
  from want
  group by group_id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1635943267433.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65343i6A9D2E34F33AA499/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1635943267433.png" alt="Patrick_0-1635943267433.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Max-Count-Grouped-by-Time-Period/m-p/778166#M247695</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-11-03T12:41:19Z</dc:date>
    </item>
  </channel>
</rss>

