Announcement

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

  • Removing sets of string characters

    Hello,

    I have a string variable (positiveassays) that contains a list of drugs and the levels of drugs detected in the specimen. I am trying to separate out the drug types into separate variables and remove the parentheses and the numeric values within the parentheses as well. Here is a sample of the cases i am working with:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str544 positiveassays
    "Hydrocodone (388.264)"                                              
    "Methamphetamine (246.86)"                                           
    "Amphetamine (1600)"                                                 
    "Cocaine (800), Benzoylecgonine (800)"                               
    "THC (10.96)"                                                        
    "Methamphetamine (1600), Hydrocodone (29.156), Amphetamine (116.379)"
    "Methamphetamine (242.75)"                                           
    "Codeine (275.445), Morphine (800), 6-MAM  (40)"                     
    "Cocaine (564.218)"                                                  
    "Methamphetamine (249.92)"                                           
    end
    I tried
    Code:
    split positiveassays, parse(,) gen(drug)
    and then
    Code:
    replace drug1 = subinstr(drug1, "(*)", "", .)
    But that didnt work. I was trying to remove the parentheses and everything in them.

    Any help would be greatly appreciated as always!

  • #2
    The people who really understand regular expressions can undoubtedly come up with a shorter solution, but this will do it:
    Code:
    replace positiveassays = ustrregexra(positiveassays, "\(\d*\.\d*\)", "")
    replace positiveassays = ustrregexra(positiveassays, "\(\d*\)", "")
    split positiveassays, parse(,) gen(drug)

    Comment


    • #3
      Thanks so much, Clyde (as usual)! This is exactly what i was looking to do.

      Comment

      Working...
      X