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

# Re: st: wildcard in numeric variable

 From Colin Campbell To "statalist@hsphsun2.harvard.edu" Subject Re: st: wildcard in numeric variable Date Fri, 1 Jun 2012 04:54:14 -0700 (PDT)

```Thanks,

An answer that stops short of 42 but works perfectly nonetheless!

Colin

----- Original Message -----
From: Nick Cox <n.j.cox@durham.ac.uk>
To: "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Cc:
Sent: Friday, 1 June 2012, 13:34
Subject: RE: st: wildcard in numeric variable

The second argument of the -string()- function if you need it should be a _numeric_ display format. That's because you are specifying how a number should be displayed. Try something like

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

for "any suitable value of 20". (I think this was a Salomon Bochner joke.)

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Colin Campbell
Sent: 01 June 2012 12:27
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: wildcard in numeric variable

Thank you Nick,

the solution

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

looks great and nearly works a treat. But, this solution will not replace those with 8 or more digits.
I assume this has to do with the str format %#s but am not sure how to specify this correctly.

I tried the following:

replace cv =. if substr(string(cv, "%12s"), 1, 4) == "9999"

but this returns "(0 real changes made)". Any other ideas?

Colin

----- Original Message -----
From: Nick Cox <njcoxstata@gmail.com>
To: statalist@hsphsun2.harvard.edu
Cc:
Sent: Friday, 1 June 2012, 10:47
Subject: Re: st: wildcard in numeric variable

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/

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

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