<?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: Select first records from the dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827923#M327040</link>
    <description>&lt;P&gt;Your rules don't make sense. The first IF is a subset of the last IF.&amp;nbsp; Perhaps you meant to include ONLY in the last one like you did for the middle one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But also your expected results do not match the rules.&lt;/P&gt;
&lt;P&gt;So 123 has both so only the XN observation should be kept, but your output is showing that one of the two IN observations was kept instead.&lt;/P&gt;
&lt;P&gt;And 125 has only IN observations, but you only selected one of them.&amp;nbsp; How did you decide which one to keep?&lt;/P&gt;</description>
    <pubDate>Tue, 09 Aug 2022 17:18:40 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-09T17:18:40Z</dc:date>
    <item>
      <title>Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827919#M327038</link>
      <description>&lt;P&gt;Hi ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need a help on this.&lt;/P&gt;&lt;P&gt;I have a dataset with the following attributes: id, typ(XN, IN), and sysdat.&lt;BR /&gt;If an ID contains both XN and IN, only XN records should be retrieved.&lt;BR /&gt;If the ID contains&amp;nbsp; XN records, only XN records must be retrieved.&lt;BR /&gt;If the ID conatins&amp;nbsp; IN records , only IN records must be retrieved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input :&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; typ&amp;nbsp; &amp;nbsp; sysdat&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; XN&amp;nbsp; &amp;nbsp;2410128&lt;/P&gt;&lt;P&gt;124&amp;nbsp; &amp;nbsp;XN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; &amp;nbsp;IN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; IN&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ouput :&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; typ&amp;nbsp; &amp;nbsp; sysdat&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;124&amp;nbsp; &amp;nbsp;XN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; &amp;nbsp;IN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 17:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827919#M327038</guid>
      <dc:creator>cho16</dc:creator>
      <dc:date>2022-08-09T17:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827923#M327040</link>
      <description>&lt;P&gt;Your rules don't make sense. The first IF is a subset of the last IF.&amp;nbsp; Perhaps you meant to include ONLY in the last one like you did for the middle one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But also your expected results do not match the rules.&lt;/P&gt;
&lt;P&gt;So 123 has both so only the XN observation should be kept, but your output is showing that one of the two IN observations was kept instead.&lt;/P&gt;
&lt;P&gt;And 125 has only IN observations, but you only selected one of them.&amp;nbsp; How did you decide which one to keep?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 17:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827923#M327040</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-09T17:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827927#M327041</link>
      <description>&lt;P&gt;Sorry typo mistake&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If an ID contains both XN and IN, only IN records should be retrieved.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 17:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827927#M327041</guid>
      <dc:creator>cho16</dc:creator>
      <dc:date>2022-08-09T17:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827928#M327042</link>
      <description>Sorry typo mistake...If an ID contains both XN and IN, only XN records should be retrieved.</description>
      <pubDate>Tue, 09 Aug 2022 17:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827928#M327042</guid>
      <dc:creator>cho16</dc:creator>
      <dc:date>2022-08-09T17:24:19Z</dc:date>
    </item>
    <item>
      <title>Select a first observations from the data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827952#M327077</link>
      <description>&lt;P&gt;Hi ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need a help on this.&lt;/P&gt;&lt;P&gt;I have a dataset with the following attributes: id, typ(XN, IN), and sysdat.&lt;BR /&gt;If an ID contains both XN and IN, only IN records should be retrieved.&lt;BR /&gt;If the ID contains&amp;nbsp; XN records, only XN records must be retrieved.&lt;BR /&gt;If the ID conatins&amp;nbsp; IN records , only IN records must be retrieved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input :&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; typ&amp;nbsp; &amp;nbsp; sysdat&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; XN&amp;nbsp; &amp;nbsp;2410128&lt;/P&gt;&lt;P&gt;124&amp;nbsp; &amp;nbsp;XN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; &amp;nbsp;IN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; IN&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ouput :&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; typ&amp;nbsp; &amp;nbsp; sysdat&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp;IN&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;124&amp;nbsp; &amp;nbsp;XN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;125&amp;nbsp; &amp;nbsp;IN&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;2210128&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 19:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827952#M327077</guid>
      <dc:creator>cho16</dc:creator>
      <dc:date>2022-08-09T19:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827953#M327058</link>
      <description>&lt;P&gt;I'm as confused about the rules here as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, so this is somewhat a shot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID typ $ sysdat;
datalines;
123 IN 2210128 
123 IN 2210128 
123 XN 2410128 
124 XN 2210128 
125 IN 2210128 
125 IN 2210128 
;

proc sql;
   create table want as
   select distinct * from have 
   group by ID
   having typ = min(typ)
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID   typ  sysdat
123  IN   2210128  
124  XN   2210128 
125  IN   2210128 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 19:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827953#M327058</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-09T19:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Select a first observations from the data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827954#M327078</link>
      <description>&lt;P&gt;I'm as confused about the rules here as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;in the other thread.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID typ $ sysdat;
datalines;
123 IN 2210128 
123 IN 2210128 
123 XN 2410128 
124 XN 2210128 
125 IN 2210128 
125 IN 2210128 
;

proc sql;
   create table want as
   select distinct * from have 
   group by ID
   having typ = min(typ)
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID   typ  sysdat
123  IN   2210128  
124  XN   2210128 
125  IN   2210128 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 19:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/827954#M327078</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-09T19:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828021#M327079</link>
      <description>&lt;P&gt;I have merged your posts. Please don't double-post question.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 04:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828021#M327079</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-10T04:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828024#M327080</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/73714"&gt;@cho16&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Sorry typo mistake...If an ID contains both XN and IN, only XN records should be retrieved.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then your output for 123 is wrong.&lt;/P&gt;
&lt;P&gt;Please review your requirements and expected output so they make sense. In particular, define a selection rule when multiple observations meet the condition.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 05:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828024#M327080</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-10T05:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select first records from the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828078#M327109</link>
      <description>&lt;PRE&gt;data have;
input ID typ $ sysdat;
datalines;
123 IN 2210128 
123 IN 2210128 
123 XN 2410128 
124 XN 2210128 
125 IN 2210128 
125 IN 2210128 
;

proc sql;
create table want as
select distinct *,case when count(distinct typ)&amp;gt;1 and typ='IN' then 1
when count(distinct typ)=1 then 1 else 0 end as flag
 from have
  group by id
   having calculated flag=1
;
quit;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 12:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828078#M327109</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-10T12:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Select a first observations from the data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828092#M327112</link>
      <description>&lt;P&gt;For each ID, interleave all the observations such that any "IN" records will precede any "XN" records.&amp;nbsp; Then just keep the first individual record for each ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID typ $ sysdat;
datalines;
123 IN 2210128 
123 IN 2210128 
123 XN 2410128 
124 XN 2210128 
125 IN 2210128 
125 IN 2210128 
run;
data want;
  set have (where=(typ='IN'))
      have (where=(typ='XN'));
  by id;
  if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes dataset HAVE is sorted by ID.&amp;nbsp; But it doesn't matter what the order is within each ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 13:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-first-records-from-the-dataset/m-p/828092#M327112</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-10T13:39:20Z</dc:date>
    </item>
  </channel>
</rss>

