<?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: Calculate Min &amp;amp; Max by groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743447#M232777</link>
    <description>&lt;P&gt;Hi....I am trying to calculate the Minimum Start and Maximum End dates for each ID and Program. Because each course can be taken in different years, the group for the minimum and maximum is the same Year and if the Years are consecutive. Once there is a missing Year for the Program, then the minimum and maximum is calculates as a new group. What is the best way to set this up. Thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Have;
length ID $3 Year $ 4 Program $ 15 Course $ 5 Start $ 10 End $ 10;
   infile datalines delimiter='' missover dsd; 
   input ID $ Year : $char4. Program : $char15. Course : $char5. Start : $char10. End : $char10.;
   datalines; 
100 2016 AutoMechanics A 2016-11-03 2017-04-20
100 2018 AutoMechanics B 2018-09-07 2019-04-20
100 2018 AutoMechanics C 2018-10-15 2019-06-25
100 2019 AutoMechanics D 2019-10-17 2020-01-20
100 2020 AutoMechanics E 2020-08-27 2020-09-15
;
run;

Want:

ID  Year Program       Course     Start      End       Min_Start    Max_End
100 2016 AutoMechanics   A     2016-11-03 2017-04-20  2016-11-03  2017-04-20
100 2018 AutoMechanics   B     2018-09-07 2019-04-20  2018-09-07  2020-09-15
100 2018 AutoMechanics   C     2018-10-15 2019-06-25  2018-09-07  2020-09-15
100 2019 AutoMechanics   D     2019-10-17 2020-01-20  2018-09-07  2020-09-15
100 2020 AutoMechanics   E     2020-08-27 2020-09-15  2018-09-07  2020-09-15&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 May 2021 00:29:45 GMT</pubDate>
    <dc:creator>twildone</dc:creator>
    <dc:date>2021-05-25T00:29:45Z</dc:date>
    <item>
      <title>RE: Calculate Min &amp; Max by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743447#M232777</link>
      <description>&lt;P&gt;Hi....I am trying to calculate the Minimum Start and Maximum End dates for each ID and Program. Because each course can be taken in different years, the group for the minimum and maximum is the same Year and if the Years are consecutive. Once there is a missing Year for the Program, then the minimum and maximum is calculates as a new group. What is the best way to set this up. Thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Have;
length ID $3 Year $ 4 Program $ 15 Course $ 5 Start $ 10 End $ 10;
   infile datalines delimiter='' missover dsd; 
   input ID $ Year : $char4. Program : $char15. Course : $char5. Start : $char10. End : $char10.;
   datalines; 
100 2016 AutoMechanics A 2016-11-03 2017-04-20
100 2018 AutoMechanics B 2018-09-07 2019-04-20
100 2018 AutoMechanics C 2018-10-15 2019-06-25
100 2019 AutoMechanics D 2019-10-17 2020-01-20
100 2020 AutoMechanics E 2020-08-27 2020-09-15
;
run;

Want:

ID  Year Program       Course     Start      End       Min_Start    Max_End
100 2016 AutoMechanics   A     2016-11-03 2017-04-20  2016-11-03  2017-04-20
100 2018 AutoMechanics   B     2018-09-07 2019-04-20  2018-09-07  2020-09-15
100 2018 AutoMechanics   C     2018-10-15 2019-06-25  2018-09-07  2020-09-15
100 2019 AutoMechanics   D     2019-10-17 2020-01-20  2018-09-07  2020-09-15
100 2020 AutoMechanics   E     2020-08-27 2020-09-15  2018-09-07  2020-09-15&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 May 2021 00:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743447#M232777</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-05-25T00:29:45Z</dc:date>
    </item>
    <item>
      <title>RE: Calculate Min &amp; Max by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743473#M232790</link>
      <description>&lt;P&gt;You say "missing year" but do not show any "missing" values for year. Do you mean a year out of sequence?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would likely be a lot easier if the year and dates were actually numeric and dates an not character values as then you could use something like Proc Summary with the dates after inserting a grouping variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This duplicates your desired output.&lt;/P&gt;
&lt;PRE&gt;data Have;
length ID $3 Year $ 4 Program $ 15 Course $ 5 Start $ 10 End $ 10;
   infile datalines delimiter='' missover dsd; 
   input ID $ Year : $char4. Program : $char15. Course : $char5. Start : $char10. End : $char10.;
   datalines; 
100 2016 AutoMechanics A 2016-11-03 2017-04-20
100 2018 AutoMechanics B 2018-09-07 2019-04-20
100 2018 AutoMechanics C 2018-10-15 2019-06-25
100 2019 AutoMechanics D 2019-10-17 2020-01-20
100 2020 AutoMechanics E 2020-08-27 2020-09-15
;
run;

data need;
   set have;
   by id year;
   Retain ordergroup;
   yeardif = dif(year);
   if first.id then ordergroup=1;
   if yeardif&amp;gt;1 then ordergroup+1;
   drop yeardif;
run;

proc sql;
   create table want as
   select a.*,b.min_start,b.max_end
               
   from need as a left join
        (select id,ordergroup,min(start) as min_start, max(end) as max_end
                from need
                group by id, ordergroup
        ) as b
        on a.id=b.id
        and a.ordergroup=b.ordergroup
   ;
quit;
&lt;/PRE&gt;
&lt;P&gt;Examples with only one set of "groups" such as Id and Program are poor examples as I do not know if ID is unique for Program or not.&lt;/P&gt;
&lt;P&gt;Also, bad things come to people who insist on storing dates as character values. None of the SAS functions or formats that work with dates will work with characters meaning that much work may be needed to get actual dates before most types of valid comparisons, intervals or grouping is done.&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 05:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743473#M232790</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-25T05:03:21Z</dc:date>
    </item>
    <item>
      <title>RE: Calculate Min &amp; Max by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743592#M232843</link>
      <description>&lt;P&gt;Hi BallardW…….Thanks for your help and your solution. It works great. Just to let you know that in the actual dataset the dates are stored in the $ymmdd10. format. When I was creating the dummy dataset, I tried using the :yymmdd10. format on the Input statement and was getting an error message&amp;nbsp;so when&amp;nbsp;I&amp;nbsp;changed the format to $char10. the error message was gone. But once again thank you so much for your help....Greatly Appreciated!!..&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 15:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Calculate-Min-amp-Max-by-groups/m-p/743592#M232843</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-05-25T15:56:01Z</dc:date>
    </item>
  </channel>
</rss>

