Announcement

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

  • String: Add blank spaces

    Good morning Stata forum,

    I have a String variable IncidentLocation. I want to use the first word of this string to concatenate it with another string. For example "Clinic (Medical Services Area)" will be "Clinic". the problem is that some string does not have spances between characters. Ex. Bridge/Vestibule will return Bridge/Vestibule and I only need Bridge. How can I add spaces between "/" .

    local aword "[^ ]* *"
    gen incident= trim(regexs(1)) if regexm(IncidentLocation, "(`aword')")

    Thank you,
    Marvin

  • #2
    If your issue is only with "/", then the following will work.

    To replace the first occurrence of "/" with " / " (space on either side of /"):

    Code:
    gen newIncidentLocation= subinstr(IncidentLocation , "/" , " / ", 1)
    To replace all occurrences of "/" with " / " (space on either side of /"):

    Code:
    gen newIncidentLocation= subinstr(IncidentLocation , "/" , " / ", .)
    However, if you have a long list of word delimiting characters, you could take several approaches, including writing a loop with subinstr, using tokenize, or using regular expressions.
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Thank you so much! I appreciate it!

      Comment

      Working...
      X