Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Extracting Numbers from Strings


From   "Sergiy Radyakin" <[email protected]>
To   [email protected]
Subject   Re: st: Extracting Numbers from Strings
Date   Wed, 24 Dec 2008 15:23:17 -0500

Hi,

if you know the number is there and everything is going to be fine
than one line is enough:
generate y=real(substr(x,strpos(x,"-")+1,length(x)-strpos(x,"-")))

otherwise you may be more cautios:
generate y=substr(x,strpos(x,"-")+1,length(x)-strpos(x,"-"))
destring y, generate(y_num)

Best wishes,
   Sergiy Radyakin

On Wed, Dec 24, 2008 at 3:09 PM,  <[email protected]> wrote:
> I think I'm interested in something more general. I'm trying to capture the
> first number after the hyphen (wherever the hyphen may be and without any
> knowledge of what comes after the number following the hyphen).
>
> However, for right now this should work (note that a negative sign is needed
> to grab from the back of the string):
> gen y=substr(d,-1,1)
>
>
>>  gen y = substr(x,1,1)
>>  destring y, replace
>>   should do it.
>>        Robert
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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