Announcement

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

  • Keep string after charachter

    Hello everyone, I have a variable called region which I would like to fix. I would like to keep the string after the ":" character.
    My data looks like the following


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str60 region
    "AD: Sant Julià de Lòria"             
    "AD: Escaldes-Engordany"               
    "AD-MD-TD: Encamp"               
    "AD: Andorra la Vella"     
    end
    I just want to replace this variable with a corrected one where AD: Andorra la Vella will be only Andorra la Vella. Same thing for other regions.

    Thank you for your time

  • #2
    Code:
    gen int  position = strpos(region, ":")
    clonevar wanted = region
    replace wanted = substr(wanted, position+1, .)

    Comment


    • #3
      Code:
      replace region = substr(region, strpos(region, ": ") + 2 , . )
      Code:
      replace region  = ustrregexrf(region,"^.*: ", "")
      Code:
      split region , parse(": ")
      replace region = region2

      Comment


      • #4
        Thank you very much, Clyde Schechter and Bjarte Aagnes . I really appreciate it.

        Comment

        Working...
        X