Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extracting unique elements of a string

    Dear Statalisters,

    I have a string variable containing letters seperated by the ";" sign. Here are a few rows:

    code
    L; O; O
    F; F; F
    E
    H; H; O

    I would like to keep only one copy of each letter. Here is the desired result for the example above:

    code_new
    L; O
    F
    E
    H; O

    I can work around absence of spaces for the semicolon sign, I just want to get rid of duplicates inside the string.

    Thank you in advance for your help.

  • #2
    Here is a brute-force approach, based on the example

    Code:
    generate code_new = code
    
    forvalues i = 1/`=c(N)' {
        
        local line = subinstr(code[`i'],";"," ",.)
        
        local line : list uniq line
        
        replace code_new = subinstr("`line'"," ","; ",.) in `i'
        
    }

    Comment

    Working...
    X