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   "Michael Blasnik" <michael.blasnik@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: how to extract numeric part of a string
Date   Mon, 16 Dec 2002 18:58:29 -0500

"Plummer, Dale" <Dale.Plummer@Vanderbilt.edu> asked

> 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                 .
>
> Thanks for any ideas.


I have needed this function and include below a small ado that does this
(although you could grab just the loop and type it interactively instead):

program define extrnum
version 7
syntax varlist(max=1) , gen(str)
local maxlen: type `varlist'
local maxlen=substr("`maxlen'",4,.)
tempvar work
qui gen str1 `work'=""
forvalues i=1/`maxlen' {
 qui replace `work'=`work'+substr(`varlist',`i',1) if
real(substr(`varlist',`i',1)<.
}
gen `gen'=real(`work')
end

*Warning: you should check to make sure that email doesn't split the long
line in the forvalues loop (there should be just a single command in the
loop).

for your example, the syntax would be:

extrnum Var_source, gen(Var_target)

You should be aware that this will simply pull together all numbers in the
variable of interest, regardless of any intervening characters, so 12test34
would become 1234.  Also, it will not extract negative signs, so all results
will be positive.

Michael Blasnik
michael.blasnik@verizon.net


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index