## Top 3 rows meet certain criteria for each ID

Solved
Occasional Contributor
Posts: 15

# Top 3 rows meet certain criteria for each ID

[ Edited ]

Hello,

I have three datasets, and what I want to do is to get 3 records for each ID on specific conditions.
My conditions are
1. Top 3 records for each ID.  AND
2. Of the 3 records, there must be one that meets the criteria which is ID’s group = product’s group. If not,  then look down to find the first match, replacing the third record of that ID.

I do know that how to get the top3 records, but don’t know how to meet the second condition…
If you have any ideas or solutions, please advise me. Much Thanks.

Here is my example:

``````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
;``````
``````data ID_group;
input ID \$ group \$;
datalines;
001 x
002 y
003 x
004 y
005 y
;``````
``````data product_group;
input product \$ group \$;
a1 x
a2 y
a3 x
a4 x
a5 y
a6 x
a7 y
;``````

So the output result is as follows:
ID product
001 a1
001 a2
001 a4
002 a1
002 a3
002 a5
003 a7
;

I tried to get the top 3 records, and also, I tried to get the first match record.
But I do not know how to meet these two conditions at once or any efficient solutions for my question. Thanks for your reading. ^_^

Accepted Solutions
Solution
‎01-23-2018 09:46 AM
Posts: 5,405

## Re: Top 3 rows meet certain criteria for each ID

Try this:

``````
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;
found = found or ok;
end;
drop ok i found;
run;
``````
PG

All Replies
Solution
‎01-23-2018 09:46 AM
Posts: 5,405

## Re: Top 3 rows meet certain criteria for each ID

Try this:

``````
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;
found = found or ok;
end;
drop ok i found;
run;
``````
PG
Occasional Contributor
Posts: 15

## Re: Top 3 rows meet certain criteria for each ID

Thank you very much. Sorry for the late reply. It helps me a lot ^_^
☑ This topic is solved.

Discussion stats
• 2 replies
• 245 views
• 0 likes
• 2 in conversation