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: Generate random missing values within a set of variables


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Generate random missing values within a set of variables
Date   Sun, 15 Apr 2012 15:26:32 +0200

On Sun, Apr 15, 2012 at 2:06 PM, Sofia Ramiro wrote:
> I have 6 variables (without missings) and I would like to randomly generate one missing value in one of the 6 variables per line/observation. I know that with the runiform command we manage to choose observations randomly within one variable (so I could generate random missing values within one variable), but how can I choose randomly one variable out of the 6 to be transformed into missing and make sure that one of them is being transformed per observation?

*---------- begin example ------------
// create some data
drop _all
set obs 100
forvalues i =1/6{
	gen x`i' = rnormal()
}

// make a variable indicating
// which variable will become missing
gen tomis = ceil(6*runiform())

// turn for each observation the
// appropriate variable to missing
forvalues i = 1/6 {
	replace x`i' = . if tomis == `i'
}

// see the result
format x* %6.3g
list in 1/10, clean
*------------- end example -------------

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------

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