Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: puzzling string conversion


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: puzzling string conversion
Date   Thu, 10 Feb 2011 15:14:42 +0000

<>
Dimitri:

I'm not sure how to fix your regular expression, but you could get by with just the -destring- with the ignore option specified:

*********!
clear
input str30 mystring
"A"
"011.xyz.22.2/33-33"
"101.abc.22.2/33-33"
"222.foo.22.2/33-33"
"111.bla.22.2/33-33"
end

destring mystring, gen(numid) ///
	 ignore("/-.,`c(alpha)'`c(ALPHA)'")
format numid %010.0f
*********!


- Eric
_
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Feb 10, 2011, at 8:57 AM, Dimitri Szerman wrote:

> Hi again,
> 
> I got this puzzling result. I have a string variable, mystring, which
> has both numeric and non-numeric characters. I'd like to extract only
> the numeric ones, and form a numeric variable with this (in fact, it's
> going to be an id). I'm using regular expressions, and this is what
> I'm doing
> 
> input str30 mystring
> "111.aaa.22.2/33-33"
> "011.xyz.22.2/33-33"
> "101.abc.22.2/33-33"
> "222.foo.22.2/33-33"
> "111.bla.22.2/33-33"
> end
> 
> gen id = mystring
> while regexm(id, "[^0-9]" ) {
> replace id = regexr(id,"[^0-9]","")
> }
> destring id, gen(numid)
> 
> And it works fine. However, if mystring has an observation which
> contains very few (when compared to the other observations)
> non-numeric characters, this seems to break down:
> 
> clear
> input str30 mystring
> "A"
> "011.xyz.22.2/33-33"
> "101.abc.22.2/33-33"
> "222.foo.22.2/33-33"
> "111.bla.22.2/33-33"
> end
> 
> gen id = mystring
> while regexm(id, "[^0-9]" ) {
> replace id = regexr(id,"[^0-9]","")
> }
> destring id, gen(numid)
> 
> Am I missing something? Why doesn't this work? Any suggestions?
> 
> Thanks,
> Dimitri
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index