DATA Step, Macro, Functions and more

Identify duplicate if the string has same characters

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

Identify duplicate if the string has same characters

I have a dataset that looks like this

data temp;
input Inv_Code $20.;
datalines;
INV001
INV002
964987
789469
I-87700
776012
FM073026-1
14774
16316/1
27107
56549
001INV
210677
70172
FM0668#8-1
11459/1
1/61361
;

run;
I need to identify duplicates as under :
INV001
001INV
789469
etc so on and so forth

 


Accepted Solutions
Solution
‎08-24-2016 11:18 PM
Super User
Posts: 5,085

Re: Identify duplicate if the string has same characters

[ Edited ]

One approach would assign each character to a separate variable:

 

data all_characters;

set have;

array letters {20} $ 1 C1-C20;

do _n_=1 to 20;

   letters{_n_} = substr(inv_code, _n_, 1);

end;

call sortc (of C1-C20);

newkey = cat(of C1-C20);

run;

 

By sorting the characters, it becomes relatively easy to compare for the same set of characters:

 

proc sort data=all_characters;

by newkey;

run;

 

data want;

set all_characters;

by newkey;

if last.newkey=0 or first.newkey=0;

run;

View solution in original post


All Replies
Regular Contributor
Posts: 242

Re: Identify duplicate if the string has same characters

Are you considering INV001 and 001INV as similar string

964987 and 789469 as similar string.

 

What is the basis to consider two string duplicate?

Contributor
Posts: 31

Re: Identify duplicate if the string has same characters

Someone may have incorrectly written it but it would have all the caracters from the the previous one. Like 78956 to 65987 and so on.

it just may be reordered thats all

Solution
‎08-24-2016 11:18 PM
Super User
Posts: 5,085

Re: Identify duplicate if the string has same characters

[ Edited ]

One approach would assign each character to a separate variable:

 

data all_characters;

set have;

array letters {20} $ 1 C1-C20;

do _n_=1 to 20;

   letters{_n_} = substr(inv_code, _n_, 1);

end;

call sortc (of C1-C20);

newkey = cat(of C1-C20);

run;

 

By sorting the characters, it becomes relatively easy to compare for the same set of characters:

 

proc sort data=all_characters;

by newkey;

run;

 

data want;

set all_characters;

by newkey;

if last.newkey=0 or first.newkey=0;

run;

Contributor
Posts: 31

Re: Identify duplicate if the string has same characters

Thanks for the logic Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 308 views
  • 3 likes
  • 3 in conversation