Keeping only duplicates with proc sort with more than one variable

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Keeping only duplicates with proc sort with more than one variable

Hi Team,

 

I found a code to get only duplicates using proc sort.

 

But I need to get the duplicates with more variables ..example var1, var2, var3, var4 ..

 

Somebody how to do it??

 

Thanks,

 

 

proc sort
     data = test;
     by var1;
run;


data single dup;
     set test;
     by var1;
     if first.var1 and last.var1 
          then output single;
     else output dup;
run;

Accepted Solutions
Solution
‎03-28-2017 12:50 PM
Grand Advisor
Posts: 9,578

Re: Keeping only duplicates with proc sort with more than one variable

proc sort
     data = test  out=want  nouniquekey  ;
     by var1 var2 var3 var4;
run;

View solution in original post


All Replies
Grand Advisor
Posts: 10,211

Re: Keeping only duplicates with proc sort with more than one variable

You need to define what you mean by duplicate? Preferably provide some example data and an indication of which you consider duplicates.

 

 

Respected Advisor
Posts: 4,976

Re: Keeping only duplicates with proc sort with more than one variable

A small variation on your program ought to do the trick:

 

proc sort
     data = test;
     by var1 var2 var3 var4;
run;

data single dup;
     set test;
     by var1 var2 var3 var4;
     if first.var4 and last.var4 
          then output single;
     else output dup;
run;

 

 

If this doesn't do it, you might need to answer some of the questions already asked, like the meaning of a duplicate.

Solution
‎03-28-2017 12:50 PM
Grand Advisor
Posts: 9,578

Re: Keeping only duplicates with proc sort with more than one variable

proc sort
     data = test  out=want  nouniquekey  ;
     by var1 var2 var3 var4;
run;

Contributor
Posts: 42

Re: Keeping only duplicates with proc sort with more than one variable

Thanks for your solution

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 130 views
  • 0 likes
  • 4 in conversation