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: Fwd: Fastest way to identify values that start and end with a 9?
From
Daniel Feenberg <[email protected]>
To
[email protected]
Subject
Re: st: Fwd: Fastest way to identify values that start and end with a 9?
Date
Thu, 3 Oct 2013 07:33:47 -0400 (EDT)
On Thu, 3 Oct 2013, Evan DeFilippis wrote:
Values in my data set contain different numerical representations for
"Don't Know" and "Refusal"
A "Don't Know" will always start and end with a '9', but there can be
as many '9's in between as possible, up to the maximum length of a
string (244).
A "Refusal" will always start with a '9' and end with an '8', and
there can be as many '9's' in between as possible, up to the maximum
length of a string (244).
The data set contains strings, integers, bytes, etc..
I want to be able to convert the numerical representations of 'Don't
Know' and 'Refusal's' into DK and REF, respectively.
My current strategy for doing this looks like so:
quietly tostring _all, replace
ds, has(type string)
di "`r(varlist)'"
unab string_vars : `r(varlist)'
foreach j in `string_vars' {
quietly replace `j'= regexr(`j', "^[9]*[9]$","DK")
quietly replace `j' = regexr(`j', "^[9]*[8]$", "REF")
}
However, this is slow because it converts the entire data set into
strings, which takes about 5 minutes, and then it has to do has(type
string) in order to get r(varlist) to iterate over all those strings
which takes about 4 minutes.
Is there a faster way to do this that perhaps does not involve
converting everything to strings?
If you wish to end up with "DK" and "REF" as values, doesn't that make
5 minutes for conversion to strings inevitable?
dan feenberg
Paul
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/