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. ^_^
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;
else if not found and ok then output;
found = found or ok;
end;
drop ok i found;
run;
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;
else if not found and ok then output;
found = found or ok;
end;
drop ok i found;
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.