Announcement

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

  • Cutting a string variable

    Hi all,

    I am trying to cut a section of a string variable up, specifically variables with "C/V" within the observation.
    I used regexm to first identify which observations had "C/V", and can use subinstr to remove "C/V", however I need to remove "C/V" along with the text/numbers that follow right after, up until a punctuation is encountered.

    The sample of the data that I have looks like this

    Avda. Crevillente C/V Madrid, 2
    Avda. Cartagena C/V San Francisco
    Pza. De Las Malvas C/V Ferris, 17

    The output I would like to get from the command will be

    Avda. Crevillente, 2
    Avda. Cartagena
    Pza. De Las Malvas, 17

    Help will be much appreciated! Thank you!

  • #2
    Please use dataex in the future. See
    ssc install dataex
    and
    help dataex
    . It'll make using your data a lot easier.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str33 var1
    "Avda. Crevillente C/V Madrid, 2"  
    "Avda. Cartagena C/V San Francisco"
    "Pza. De Las Malvas C/V Ferris, 17"
    end
    Try:
    Code:
    replace var1 = regexr(var1, " C\/V [^a]*", "")

    Comment


    • #3
      Please use dataex in the future. See ssc install dataex and help dataex. Here is your data:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str33 var1
      "Avda. Crevillente C/V Madrid, 2"  
      "Avda. Cartagena C/V San Francisco"
      "Pza. De Las Malvas C/V Ferris, 17"
      end
      Try:
      Code:
      replace var1 = regexr(var1, " C\/V [^.,]*", "")
      You may need to add more punctuation, as needed.

      Comment

      Working...
      X