Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: how to extract numeric part of a string


From   "Scott Merryman" <[email protected]>
To   <[email protected]>
Subject   st: Re: how to extract numeric part of a string
Date   Mon, 16 Dec 2002 17:49:33 -0600

----- Original Message -----
From: "Plummer, Dale" <[email protected]>
To: <[email protected]>
Sent: Monday, December 16, 2002 4:22 PM
Subject: st: how to extract numeric part of a string


> If I have a string variable, is there a way to extract out only the
> number component?
>
> Examples:
>
> Var_source        Var_target
> Abc123             123
> Dog34              34
> 1209               1209
> cat                 .
>
As brute force method you can use the destring command with the ignore option.
However, you have to specify all the characters to be ignored.  E.g.

. list

          var1
  1.    abc123
  2.    ab1234
  3.     abcd1
  4.    a12345
  5.    142332

. destring var1 , gen(new) i(a b c d )
var1: characters a b c d removed; new generated as long

. list

          var1         new
  1.    abc123         123
  2.    ab1234        1234
  3.     abcd1           1
  4.    a12345       12345
  5.    142332      142332

Scott



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index