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]

st: Fwd: Fastest way to identify values that start and end with a 9?


From   Evan DeFilippis <[email protected]>
To   [email protected]
Subject   st: Fwd: Fastest way to identify values that start and end with a 9?
Date   Thu, 3 Oct 2013 12:08:48 +0300

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?

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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index