Re: Remove repeated "words" in a string variable

Posted by Andy W on
URL: http://spssx-discussion.165.s1.nabble.com/Remove-repeated-words-in-a-string-variable-tp5740541p5740542.html

The python code at the link can be modified slightly to do what you want.
Note that this does not retain the same order of the ICD codes, so if the
first one is the primary or whatever it may shift it.

***************************************************.
DATA LIST LIST / id (F1.0) icd(a100).
BEGIN DATA
1 "T079 S370 S220 S270 S220 S369 T079 T079 S370 S220 "
2 "J809B N179 J969 R572 J459 J159 J969 J809C R651 N179 "
3 "I609 N179 R572 J809C B371 I609 N179 N179 I609"
END DATA.
DATASET NAME ICD.
EXECUTE.

BEGIN PROGRAM PYTHON.
# Defining a function to delete duplicate substrings
def del_sub(x):
    split_str = x.split(" ") #doesnt keep the same order
    return " ".join(set(split_str)).strip()

# Example    
te = "I609 N179 R572 J809C B371 I609 N179 N179 I609"
print( del_sub(te) )
END PROGRAM.

SPSSINC TRANS RESULT=NoDupICD TYPE=100
  /FORMULA "del_sub(x=icd)".
***************************************************.




-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/