<?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 Top 3 rows meet certain criteria for each ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/423401#M104104</link>
    <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I have three datasets, and what I want to do is to get 3 records for each ID on specific conditions.&lt;BR /&gt;My conditions are&lt;BR /&gt;1. Top 3 records for each ID.&amp;nbsp; AND&lt;BR /&gt;2. Of the 3 records, there must be one that meets the criteria which is ID’s group = product’s group. If not,&amp;nbsp; then look down to find the first match, replacing the third record of that ID.&lt;BR /&gt;&lt;BR /&gt;I do know that how to get the top3 records, but don’t know how to meet the second condition…&lt;BR /&gt;If you have any ideas or solutions, please advise me. Much Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data report; 
input id $ product $ score ; 
datalines;
001 a1 20
001 a2 10
001 a4 9
001 a5 8
001 a7 7
002 a1 99
002 a3 10
002 a4 8
002 a5 3
002 a7 1
003 a7 10
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data ID_group;
input ID $ group $;
datalines;
001 x
002 y
003 x
004 y
005 y
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data product_group;
input product $ group $;
a1 x
a2 y
a3 x
a4 x
a5 y
a6 x
a7 y
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So the output result is as follows:&lt;BR /&gt;ID product&lt;BR /&gt;001 a1&lt;BR /&gt;001 a2&lt;BR /&gt;001 a4&lt;BR /&gt;002 a1&lt;BR /&gt;002 a3&lt;BR /&gt;002 a5&lt;BR /&gt;003 a7&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;I tried to get the top 3 records, and also, I tried to get the first match record.&lt;BR /&gt;But I do not know how to meet these two conditions at once or any efficient solutions for my question. Thanks for your reading. ^_^&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 23 Dec 2017 02:33:55 GMT</pubDate>
    <dc:creator>wenzli25</dc:creator>
    <dc:date>2017-12-23T02:33:55Z</dc:date>
    <item>
      <title>Top 3 rows meet certain criteria for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/423401#M104104</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I have three datasets, and what I want to do is to get 3 records for each ID on specific conditions.&lt;BR /&gt;My conditions are&lt;BR /&gt;1. Top 3 records for each ID.&amp;nbsp; AND&lt;BR /&gt;2. Of the 3 records, there must be one that meets the criteria which is ID’s group = product’s group. If not,&amp;nbsp; then look down to find the first match, replacing the third record of that ID.&lt;BR /&gt;&lt;BR /&gt;I do know that how to get the top3 records, but don’t know how to meet the second condition…&lt;BR /&gt;If you have any ideas or solutions, please advise me. Much Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data report; 
input id $ product $ score ; 
datalines;
001 a1 20
001 a2 10
001 a4 9
001 a5 8
001 a7 7
002 a1 99
002 a3 10
002 a4 8
002 a5 3
002 a7 1
003 a7 10
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data ID_group;
input ID $ group $;
datalines;
001 x
002 y
003 x
004 y
005 y
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class="language-sas"&gt;data product_group;
input product $ group $;
a1 x
a2 y
a3 x
a4 x
a5 y
a6 x
a7 y
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So the output result is as follows:&lt;BR /&gt;ID product&lt;BR /&gt;001 a1&lt;BR /&gt;001 a2&lt;BR /&gt;001 a4&lt;BR /&gt;002 a1&lt;BR /&gt;002 a3&lt;BR /&gt;002 a5&lt;BR /&gt;003 a7&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;I tried to get the top 3 records, and also, I tried to get the first match record.&lt;BR /&gt;But I do not know how to meet these two conditions at once or any efficient solutions for my question. Thanks for your reading. ^_^&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Dec 2017 02:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/423401#M104104</guid>
      <dc:creator>wenzli25</dc:creator>
      <dc:date>2017-12-23T02:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Top 3 rows meet certain criteria for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/423405#M104107</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table full as
select a.*,
    b.group = c.group as ok
from report as a left join 
    id_group as b on a.id=b.id left join
    product_group as c on a.product=c.product
order by id, product;
quit;

data want;
do i = 1 by 1 until(last.id);
    set full; by id;
    if i in (1, 2) then output;
    else if i = 3 and (found or ok) then output; 
    else if not found and ok then output;
    found = found or ok;
    end;
drop ok i found;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Dec 2017 04:25:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/423405#M104107</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-12-23T04:25:24Z</dc:date>
    </item>
    <item>
      <title>Re: Top 3 rows meet certain criteria for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/430006#M106252</link>
      <description>Thank you very much. Sorry for the late reply. It helps me a lot ^_^</description>
      <pubDate>Tue, 23 Jan 2018 14:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-3-rows-meet-certain-criteria-for-each-ID/m-p/430006#M106252</guid>
      <dc:creator>wenzli25</dc:creator>
      <dc:date>2018-01-23T14:47:47Z</dc:date>
    </item>
  </channel>
</rss>

