Announcement

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

  • Changing semicolons to commas (or vice versa) in a string variable

    Hi everyone,

    I have a variable in my STATA dataset which (often) has numerous numbers separated by both semicolons and commas. For example, one observation for this variable might read 14,15,17;2;9;12. I would like to separate these numbers out into a new variable, and I think a sensible first step would be to change the semicolons to commas (or vice versa) before doing so, because if I simply split variable, p(",") or split variable, p(";"), the numbers don't split correctly if there is a number between a semicolon and comma. Does anyone know how to do this or have a workaround to my problem?

    Warm regards, Oliver

  • #2
    Code:
     replace foo = subinstr(foo, ";", ",", .) 
    first is one solution, but a better one is documented:

    Code:
     split foo, parse(, ;)
    See also https://www.statalist.org/forums/help#spelling
    Last edited by Nick Cox; 02 Sep 2023, 05:26.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
       replace foo = subinstr(foo, ";", ",", .) 
      first is one solution, but a better one is documented:

      Code:
       split foo, parse(, ;)
      See also https://www.statalist.org/forums/help#spelling
      Many thanks Nick, much appreciated. Oliver

      Comment

      Working...
      X