<?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: Find Earliest Date in Latest Group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464273#M285087</link>
    <description>&lt;P&gt;If you would rather not use SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by subject regimen date; run;

proc means data=have noprint;
by subject;
output out=temp(drop=_: ) maxid(date(regimen))=  / autoname;
run;

data want;
merge have temp(in=ok); by subject regimen;
if ok and first.regimen then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 23 May 2018 04:00:17 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-05-23T04:00:17Z</dc:date>
    <item>
      <title>Find Earliest Date in Latest Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464207#M285083</link>
      <description>&lt;P&gt;I am trying to find the first date of the latest regimen in a group of treatments.&amp;nbsp; I'm having a hard time wrapping my head around this.&amp;nbsp; I have a feeling PROC SQL with a HAVING statement might be my best bet, but I'm not able to think of how to do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I have several subjects who have received various regimens of therapy where each regimen can contain multiple therapies.&amp;nbsp; I want to find the latest date for each subject.&amp;nbsp; Then, I need to find the earliest date in that corresponding regimen.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, in the data below, Subject 101-001 has a latest date of 04JUL2014, which is Regimen #2.&amp;nbsp; The earliest date in Regimen #2 is 10JUN2014.&amp;nbsp; That would be the record I want to keep.&amp;nbsp; Likewise, Subject 101-002 has a latest date of 21DEC2016, which&amp;nbsp;is in Regimen #2.&amp;nbsp; The earliest date in Regimen #2 is 15NOV2016.&amp;nbsp; That would be the record I would want to keep.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't make any assumptions like regimens being in chronological order.&amp;nbsp; It could very well happen that Regimen #2 happens a couple years before Regimen #1.&amp;nbsp; Regimens are the order they are entered more so than when they occurred.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject Regimen Date&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;12-Dec-2013&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;21-Dec-2013&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5-Jan-2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;10-Jun-2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;19-Jun-2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-001&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4-Jul-2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;27-Sep-2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1-Oct-2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;15-Nov-2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;21-Dec-2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;20-Nov-2016&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1-Dec-2016&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 22 May 2018 21:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464207#M285083</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-05-22T21:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find Earliest Date in Latest Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464223#M285085</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject $ Regimen Date :date9.;
format date date9.;
cards;
101-001	1	12-Dec-2013
101-001	1	21-Dec-2013
101-001	1	5-Jan-2014
101-001	2	10-Jun-2014
101-001	2	19-Jun-2014
101-001	2	4-Jul-2014
101-002	1	27-Sep-2016
101-002	1	1-Oct-2016
101-002	2	15-Nov-2016
101-002	2	21-Dec-2016
101-002	3	20-Nov-2016
101-002	3	1-Dec-2016
;
proc sql;
create table want as
select a.*
from have a, (select subject,regimen as _regimen  from have group by subject having date=max(date)) b 
where a.subject=b.subject
group by a.subject,regimen,_regimen 
having date=min(date) and _regimen=regimen;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 May 2018 22:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464223#M285085</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-22T22:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Find Earliest Date in Latest Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464273#M285087</link>
      <description>&lt;P&gt;If you would rather not use SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by subject regimen date; run;

proc means data=have noprint;
by subject;
output out=temp(drop=_: ) maxid(date(regimen))=  / autoname;
run;

data want;
merge have temp(in=ok); by subject regimen;
if ok and first.regimen then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 04:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464273#M285087</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-05-23T04:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find Earliest Date in Latest Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464295#M285088</link>
      <description>&lt;P&gt;If you only want the date (and not other variables from the first observation in the regimen), it can be done in a single datastep like this:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; do until(last.subject);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; set have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; by subject regimen;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; if first.regimen then&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; first_date=date;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; date=first_date;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; drop first_date;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 07:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464295#M285088</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-05-23T07:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: Find Earliest Date in Latest Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464360#M285089</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject $ Regimen Date :date9.;
format date date9.;
cards;
101-001	1	12-Dec-2013
101-001	1	21-Dec-2013
101-001	1	5-Jan-2014
101-001	2	10-Jun-2014
101-001	2	19-Jun-2014
101-001	2	4-Jul-2014
101-002	1	27-Sep-2016
101-002	1	1-Oct-2016
101-002	2	15-Nov-2016
101-002	2	21-Dec-2016
101-002	3	20-Nov-2016
101-002	3	1-Dec-2016
;
data temp;
 set have;
 by Subject  Regimen notsorted;
 group+first.Regimen;
run;
proc sort data=temp;
by Subject group descending date;
run;
data want;
 set temp;
 by Subject;
 if last.Subject;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 13:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-Earliest-Date-in-Latest-Group/m-p/464360#M285089</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-23T13:03:37Z</dc:date>
    </item>
  </channel>
</rss>

