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 on April 23, and its replacement, 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 <>
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 L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index