<?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 min date and max date by group, but the group appears again in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493767#M129967</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input	ID (start_date    end_date) (:yymmdd10.)       drug $;
format start_date    end_date yymmdd10.;
cards;
1 2003-01-01   2003-01-28     A
1 2003-02-01   2003-03-01     A
1 2003-03-15   2003-03-31     A
1 2003-04-11   2003-04-30     B
1 2003-06-01   2003-07-28     B
1 2003-07-31   2003-08-15     B
1 2003-09-01   2003-09-28     A
1 2003-10-01   2003-11-28     A
;

data want;
if 0 then set have;
call missing(start,end);
do until(last.drug);
set have;
by id drug notsorted;
start=min(start,start_date);
end=max(end,end_date);
end;
format start end yymmdd10.;
keep id start end drug;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 08 Sep 2018 15:36:45 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-09-08T15:36:45Z</dc:date>
    <item>
      <title>find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493756#M129956</link>
      <description>&lt;P&gt;Hi, I am working on the project and stuck at this point.&lt;/P&gt;&lt;P&gt;I have a data with id, start date, end date for prescribed drug, drug as below.&lt;/P&gt;&lt;P&gt;This is the list of treatment days for certain drug for each patient.&lt;/P&gt;&lt;P&gt;I need to keep the sequence of prescribed drug.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data 'have' is like&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID start_date&amp;nbsp; &amp;nbsp; end_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drug&lt;/P&gt;&lt;P&gt;~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/P&gt;&lt;P&gt;1 2003-01-01&amp;nbsp; &amp;nbsp;2003-01-28&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-02-01&amp;nbsp; &amp;nbsp;2003-03-01&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-03-15&amp;nbsp; &amp;nbsp;2003-03-31&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-04-11&amp;nbsp; &amp;nbsp;2003-04-30&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-06-01&amp;nbsp; &amp;nbsp;2003-07-28&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-07-31&amp;nbsp; &amp;nbsp;2003-08-15&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-09-01&amp;nbsp; &amp;nbsp;2003-09-28&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-10-01&amp;nbsp; &amp;nbsp;2003-11-28&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 2005-04-30&amp;nbsp; &amp;nbsp;2005-06-01&amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;........&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if I use the code like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table want as
     select id, min(start_date) as start, max(end_date) as end, drug
     from have
     group by drug;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would have a data like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;id start_date&amp;nbsp; &amp;nbsp;end_date&amp;nbsp; &amp;nbsp;drug&lt;/P&gt;&lt;P&gt;~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-01-01 2003-11-28 &amp;nbsp; A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-04-11&amp;nbsp;2003-08-15&amp;nbsp;&amp;nbsp; B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;this is wrong.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I want is&lt;/P&gt;&lt;P&gt;id start_date&amp;nbsp; &amp;nbsp;end_date&amp;nbsp; &amp;nbsp;drug&lt;/P&gt;&lt;P&gt;~~~~~~~~~~~~~~~~~~~~~~~~~&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;2003-01-01&amp;nbsp;2003-03-31&amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-04-11&amp;nbsp;2003-08-15&amp;nbsp;&amp;nbsp; B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp;2003-09-01&amp;nbsp;2003-11-28&amp;nbsp; &amp;nbsp;A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;........&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;.....&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I need to keep the sequence of drug and get the min. date and max. date for each sequence of drug.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;anybody know what to do? I tried first.last. retain..... etc..&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;i am really stuck here.. please help me out!!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Sep 2018 15:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493756#M129956</guid>
      <dc:creator>interism79</dc:creator>
      <dc:date>2018-09-08T15:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493767#M129967</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input	ID (start_date    end_date) (:yymmdd10.)       drug $;
format start_date    end_date yymmdd10.;
cards;
1 2003-01-01   2003-01-28     A
1 2003-02-01   2003-03-01     A
1 2003-03-15   2003-03-31     A
1 2003-04-11   2003-04-30     B
1 2003-06-01   2003-07-28     B
1 2003-07-31   2003-08-15     B
1 2003-09-01   2003-09-28     A
1 2003-10-01   2003-11-28     A
;

data want;
if 0 then set have;
call missing(start,end);
do until(last.drug);
set have;
by id drug notsorted;
start=min(start,start_date);
end=max(end,end_date);
end;
format start end yymmdd10.;
keep id start end drug;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Sep 2018 15:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493767#M129967</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-08T15:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493770#M129969</link>
      <description>&lt;P&gt;You need to create a sequence variable somehow, assuming your input data is sorted properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    previous_id=lag(id);
    previous_drug=lag(drug);
    if id=previous_id and drug^=previous_drug then sequence+1;
run;
proc summary data=have2;
    class id drug sequence;
    var start_date end_date;
    output out=want min(start_date)= max(end_date)=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Sep 2018 15:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493770#M129969</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-09-08T15:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493778#M129972</link>
      <description>&lt;P&gt;Try adding the NOTSORTED options to a BY statement within PROC SUMMARY or MEANS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have2;
  by id drug notsorted;
  var start_date end_date;
  output out=want min(start_date)= max(end_date)=; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Sep 2018 17:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/493778#M129972</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-08T17:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495635#M130849</link>
      <description>oh! Thanks Paige, I am not familiar with proc summary! but it perfectly works out! Thank you!!</description>
      <pubDate>Fri, 14 Sep 2018 11:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495635#M130849</guid>
      <dc:creator>interism79</dc:creator>
      <dc:date>2018-09-14T11:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495637#M130851</link>
      <description>"notsorted" option works like a magic! Thank you for the tips!!</description>
      <pubDate>Fri, 14 Sep 2018 11:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495637#M130851</guid>
      <dc:creator>interism79</dc:creator>
      <dc:date>2018-09-14T11:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495638#M130852</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
	set have;
	by id;
	previous_drug=lag(drug);
	if first.id then previous_drug='';
	if drug^=previous_drug then sequence+1;
	if first.id then sequence=1;
run;

proc summary data=have2;
	by id drug notsorted;
	var start_date end_date ;
	output out=want min(start_date)= max(end_date)= ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use the code above.. It works perfectly as I wanted!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I really appreciated your help!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 11:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495638#M130852</guid>
      <dc:creator>interism79</dc:creator>
      <dc:date>2018-09-14T11:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: find min date and max date by group, but the group appears again</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495640#M130853</link>
      <description>I did not try this code yet, but it looks so great!&lt;BR /&gt;I did not think about that this way at all..&lt;BR /&gt;Thank you for your help!</description>
      <pubDate>Fri, 14 Sep 2018 11:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-min-date-and-max-date-by-group-but-the-group-appears-again/m-p/495640#M130853</guid>
      <dc:creator>interism79</dc:creator>
      <dc:date>2018-09-14T11:59:23Z</dc:date>
    </item>
  </channel>
</rss>

