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

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

 From Joe Canner <[email protected]> To "[email protected]" <[email protected]> Subject st: RE: Fwd: Fastest way to identify values that start and end with a 9? Date Thu, 3 Oct 2013 13:24:15 +0000

```Evan/Paul,

I am as confused as others regarding what you are actually trying to do, but I will add this request for clarification: you say that the variables are a mixture of 8's and 9's up to a length of string(244).  This implies that they are already strings, since Stata would not be able to store a number that was 244 digits long.  Accordingly, why then do you need to convert them to string?

Regards,
Joe Canner
Johns Hopkins University School of Medicine

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Evan DeFilippis
Sent: Thursday, October 03, 2013 5:09 AM
To: [email protected]
Subject: st: Fwd: Fastest way to identify values that start and end with a 9?

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/

*
*   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/
```