Announcement

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

  • Separating a character and a number part of a variable

    I have a variable consisting of characters and numbers in a string format. How can I separate the character part of a variable and the numbers part? The positioning of a number part is not in the same sequence for all observations so that I couldn't use a substring option. Is there any general way to identify the number part and separate it?

  • #2
    Without a data example this is rather vague: do the numbers include fractional parts, for example? Here is a starter, using moss from SSC.


    Code:
    clear 
    input str42 whatever 
    "Answer is 42"
    "101 Dalmatians"
    "76 trombones"
    "Stata 18: the mystery"
    end
    
    moss whatever , match("([0-9]+)") regex 
    
    gen remainder = subinstr(whatever, _match1, "", .)
    
    list 
    
         +------------------------------------------------------------------------+
         |              whatever   _count   _match1   _pos1             remainder |
         |------------------------------------------------------------------------|
      1. |          Answer is 42        1        42      11            Answer is  |
      2. |        101 Dalmatians        1       101       1            Dalmatians |
      3. |          76 trombones        1        76       1             trombones |
      4. | Stata 18: the mystery        1        18       7   Stata : the mystery |
         +------------------------------------------------------------------------+

    Comment

    Working...
    X