<?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: How to keep only first date by ID with multiple rows with same date. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829537#M327753</link>
    <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   retain fdate;
   if first.id then do;
      fdate=date;
      output;
   end;
   else if date=fdate then output;
   drop fdate;
run;&lt;/PRE&gt;
&lt;P&gt;BY group creates variables first. and last. data that are numeric 1/0 which can be used to test if the current record is the first or last of a group for each variable on the BY statement. The values are accessed using First.variablename or Last.variablename, pay attention to that dot, it is critical. So when the first value of ID for a group is encountered store the value of DATE into a Retained variable, Fdate in this case, that will persist across data step boundaries.&lt;/P&gt;
&lt;P&gt;The explicit Output writes the First record for the ID and the Output in the Else clause only writes output to the set when the value of Date matches that of the retained value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;General hint: If your problem has anything to do with "first" anything than likely BY group processing with the First. or Last. variables&lt;/P&gt;</description>
    <pubDate>Sun, 21 Aug 2022 04:08:43 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-08-21T04:08:43Z</dc:date>
    <item>
      <title>How to keep only first date by ID with multiple rows with same date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829531#M327751</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have sample data below. I need to keep all the rows that have the first date for each ID. For example, ID 2 I want to keep both rows that have '01/25/2016' and ditch the other 2. My full data have any number of rows so this needs a general solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is appreciated. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA have;&lt;BR /&gt;input id date mmddyy10. var1 $;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 02/20/2016 A&lt;BR /&gt;1 02/25/2016 B&lt;BR /&gt;2 01/25/2016 A&lt;BR /&gt;2 01/25/2016 B&lt;BR /&gt;2 01/29/2016 B&lt;BR /&gt;2 02/04/2016 A&lt;BR /&gt;3 03/10/2016 A&lt;BR /&gt;3 03/10/2016 A&lt;BR /&gt;3 03/10/2016 B&lt;BR /&gt;3 03/22/2016 B&lt;/P&gt;
&lt;P&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;DATA want;&lt;BR /&gt;input id date mmddyy10. var1 $;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 02/20/2016 A&lt;BR /&gt;2 01/25/2016 A&lt;BR /&gt;2 01/25/2016 B&lt;BR /&gt;3 03/10/2016 A&lt;BR /&gt;3 03/10/2016 A&lt;BR /&gt;3 03/10/2016 B&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Aug 2022 00:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829531#M327751</guid>
      <dc:creator>Manhort</dc:creator>
      <dc:date>2022-08-21T00:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only first date by ID with multiple rows with same date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829537#M327753</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   retain fdate;
   if first.id then do;
      fdate=date;
      output;
   end;
   else if date=fdate then output;
   drop fdate;
run;&lt;/PRE&gt;
&lt;P&gt;BY group creates variables first. and last. data that are numeric 1/0 which can be used to test if the current record is the first or last of a group for each variable on the BY statement. The values are accessed using First.variablename or Last.variablename, pay attention to that dot, it is critical. So when the first value of ID for a group is encountered store the value of DATE into a Retained variable, Fdate in this case, that will persist across data step boundaries.&lt;/P&gt;
&lt;P&gt;The explicit Output writes the First record for the ID and the Output in the Else clause only writes output to the set when the value of Date matches that of the retained value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;General hint: If your problem has anything to do with "first" anything than likely BY group processing with the First. or Last. variables&lt;/P&gt;</description>
      <pubDate>Sun, 21 Aug 2022 04:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829537#M327753</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-21T04:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only first date by ID with multiple rows with same date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829541#M327754</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table want as
   select * from have
   group by id
   having date = min(date)
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Aug 2022 06:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829541#M327754</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-21T06:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only first date by ID with multiple rows with same date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829542#M327755</link>
      <description>&lt;P&gt;You can simplify this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;
   retain fdate;
   if first.id then fdate=date;
   if date=fdate;
   drop fdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Aug 2022 06:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829542#M327755</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-21T06:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only first date by ID with multiple rows with same date.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829549#M327760</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
input id date mmddyy10. var1 $;
format date mmddyy10.;
CARDS;
1 02/20/2016 A
1 02/25/2016 B
2 01/25/2016 A
2 01/25/2016 B
2 01/29/2016 B
2 02/04/2016 A
3 03/10/2016 A
3 03/10/2016 A
3 03/10/2016 B
3 03/22/2016 B

;
data want;
 set have;
 by id date notsorted;
 if first.id then n=0;
 n+first.date;
 if n=1;
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Aug 2022 10:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-first-date-by-ID-with-multiple-rows-with-same/m-p/829549#M327760</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-21T10:43:56Z</dc:date>
    </item>
  </channel>
</rss>

