Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: wildcard in numeric variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: wildcard in numeric variable
Date   Fri, 1 Jun 2012 09:47:11 +0100

Wildcards don't apply to numeric values.

... if substr(string(numvar), 1, 4) == "9999"

is the simplest solution that occurs to me. No need for new variables,
-tostring- or any regex machinery.

Nick

On Fri, Jun 1, 2012 at 9:40 AM, Colin Campbell
<col.campbell@rocketmail.com> wrote:
>
> Dear Statalist,
>
> I have a dataset in which a numeric variable cv contains values that need to be coded as missing.
> There are a variety of numeric values 9999, 99999, 999999 etc that need to be recoded.
> While I could replace each value individually, I wondered if there is a way to use wildcards with numeric variables such that e.g.
>
>
>     replace numvar =. if numvar == 9999*
>
> would work. I have a workaround using string variables as follows
>
>     tostring cv, gen(cv_str)
>     gen cv_mis = regexm(cv_str, "9999")
>     replace cv =. if cv_mis ==1
>     drop cv_mis cv_str
>
> Is there a more elegant way to achieve this without resorting to strings?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index