Announcement

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

  • Extract number only from string variable

    dear all

    I would like to extract only number from a string variable. For example, my variable A has an observation of "3 repeat prescription " then I would like to extract only "3" from here. Likewise, I want to have "2" from "2 Blood tests". Many thanks in advance. BW Kim

  • #2
    A short answer is that everything depends on how and how consistently your numbers occur. In your two examples, it would be enough to extract real(word(A, 1))

    A more general answer is that you may need some kind of regular expression defining how numbers occur, but in turn the details depend on whether

    1. Two or more numbers may appear,

    2, Fractional parts or even minus signs are allowed, or you are just looking for integers.

    Code:
     search regular expression
    in Stata leads to documentation.

    Comment


    • #3
      Dear Nick, Thank you very much for your reply. It is really helpful and solved my problem. Thank you! Kind regards, Kim

      Comment

      Working...
      X