Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: setting missing values to random numbers


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: setting missing values to random numbers
Date   Wed, 21 Mar 2007 13:08:32 +0100

ROBERT BOZICK wrote:
> I am working on recoding a categorical variable (orig) with 10 values into
> a binary variable (origred) coded  0,1.
>
> gen origred = .
> replace origred = 0 if orig >=1  & orig <=5
> replace origred = 1 if orig >5    & orig <=10
>
> There are a number of missing values for the original variable orig.
>
> Is there a way to have Stata randomly assign those missing values to either
> a 0 or a 1 in the new variable origred?  I am completely stumped.

You can add the line

  replace origred = floor(uniform()*2) if mi(orig)

to the commands above.

You can also replace all your commands by a one-liner:

  gen origred = cond(mi(orig),floor(uniform()*2,inrange(orig,6,10))


Uli

-- 
Ulrich Kohler
[email protected]
030/25491-361
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index