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]

Re: st: rounding values in a large subset of variables


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: rounding values in a large subset of variables
Date   Tue, 11 Feb 2014 14:49:33 +0000

-replace- is a command, not a function.

That aside, the syntax of -replace- is given by its help

   replace oldvar =exp [if] [in] [, nopromote]

so stripping off the bracketed elements, the syntax only allows one
existing variable name and correspondingly the expression to define
the newly changed variable.

You need a loop.

foreach v of var eg_rs* {
     replace `v' = round(`v')
}

However, that way you lose information. Unless you are completely sure
that this is exactly what you want, then generating new variables
would be safer:

foreach v of var eg_rs* {
     gen `v'_r = round(`v')
}

Nick
njcoxstata@gmail.com

On 11 February 2014 14:43, Emeline Gauthiez <emeline.gauthiez@gmail.com>

> I have a large subset of variables all starting with the prefix eg_rs*,
> with values between
> 0 and 2 I would like to round them all to 0, 1 or 2. (see example (actual
> dataset is much larger))
>
> I tried with the replace function :
> replace eg_rs*=round(eg_rs*)
>
> but it doesn't work...
>
> -example begin-
>
> What I have :
> id    eg_rs2660    eg_rs3452    eg_rs7894
> 1001  1            0.99         0.85
> 1002  0            1            2
> 1003  2            1.99         1
> 1004  1.12         2            2
> 1005  0.3          0            1
> 1006  1            0            0.78
>
>
> What I'd like :
> id    eg_rs2660    eg_rs3452    eg_rs7894
> 1001  1            1            1
> 1002  0            1            2
> 1003  2            2            1
> 1004  1            2            2
> 1005  0            0            1
> 1006  1            0            1
>
> -example end-
>
> Any help will be very much appreciated, thanks!
*
*   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