DATA Step, Macro, Functions and more

proc sort and dupout: how to get the pairs of duplicates

Reply
Super Contributor
Posts: 318

proc sort and dupout: how to get the pairs of duplicates

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. 

Regular Contributor
Posts: 228

Re: proc sort and dupout: how to get the pairs of duplicates

Posted in reply to fengyuwuzu

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;

 

Trusted Advisor
Posts: 1,018

Re: proc sort and dupout: how to get the pairs of duplicates

Posted in reply to fengyuwuzu

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.

Super User
Posts: 7,762

Re: proc sort and dupout: how to get the pairs of duplicates

Posted in reply to fengyuwuzu
proc sql;
create table d as
select * from a
group by id, age, sex, win
having count(*) >= 2
;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super Contributor
Posts: 318

Re: proc sort and dupout: how to get the pairs of duplicates

Posted in reply to KurtBremser

Thank you very much, all of you! It is so nice to have multiple solutions! 

Ask a Question
Discussion stats
  • 4 replies
  • 4386 views
  • 1 like
  • 4 in conversation