proc sort data=A out=B dupout=C nodupkey; By var1 var 2 var3 ; run;
by using the above code, I can get a dataset unique on By variables (dataset B) and the duplicates on the By variables (dataset C).
Sometimes I want to compare the duplicates (unique ones in B and duplicates in C), to see what variables they differ other than the By variables, but how to put them together? I mean how to extract those obs which have the matching By variables in dataset C?
for example, I have dataset A as:
ID age sex win lost
1 20 F 200 120
2 22 M 150 130
2 22 M 150 80
3 25 M 110 90
3 25 M 110 210
4 27 F 105 85
if I run
proc sort data=A out=B dupout=C nodupkey; by ID age sex win; run;
I will get B as:
ID age sex win lost
1 20 F 200 120
2 22 M 150 130
3 25 M 110 90
4 27 F 105 85
and C:
ID age sex win lost
2 22 M 150 80
3 25 M 110 210
Now I want to how other variables in the duplicates differ other than the identical By variables, so I want to have the PAIRS of duplicates like this:
ID age sex win lost
2 22 M 150 130
2 22 M 150 80
3 25 M 110 90
3 25 M 110 210
This means I need to extract the "By variable" identical obs from dataset B. How to do it? Thanks in advance.
data bygroups_having_duplicates;
set b (in=inb) c;
by id age;
if not(first.age=1 and last.age=1);
if inb then source='B';
else source='C';
run;
No singletons will pass the subsetting if statement. And the first record for each by group will be from dataset B. All subsequent records for the by group are from C.
PROC SQL alternative from @Kurt_Bremser:
proc sql;
create table d as
select * from a
group by id, age, sex, win
having count(*) >= 2
;
quit;
How about this? HTH.
proc sort data=A out=dup_rec nouniquekey; by id age sex win; run;
proc print data=dup_rec noobs; run;
data bygroups_having_duplicates;
set b (in=inb) c;
by id age;
if not(first.age=1 and last.age=1);
if inb then source='B';
else source='C';
run;
No singletons will pass the subsetting if statement. And the first record for each by group will be from dataset B. All subsequent records for the by group are from C.
PROC SQL alternative from @Kurt_Bremser:
proc sql;
create table d as
select * from a
group by id, age, sex, win
having count(*) >= 2
;
quit;
proc sql;
create table d as
select * from a
group by id, age, sex, win
having count(*) >= 2
;
quit;
Thank you very much, all of you! It is so nice to have multiple solutions!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.