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

# Re: st: Generate random missing values within a set of variables

 From Eduardo Montoya To statalist@hsphsun2.harvard.edu Subject Re: st: Generate random missing values within a set of variables Date Sun, 15 Apr 2012 09:41:23 -0700

```Generalizing using the combinations approach seems tricky.  You might take
a look at -rowranks-.

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

// make a set of variables indicating
// which variable will become missing
forval i = 1/6 {
gen unif`i' = runiform()
}
rowranks unif*, gen(rank1-rank6)

// turn for each observation the
// appropriate variable to missing
forval i = 1/6 {
replace x`i' = . if rank`i' <= 2
}

// Count missings per row
egen cntmiss = rowmiss(x*)
tab cntmiss

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

drop unif* rank*
*------------- end example -------------

On Sun, Apr 15, 2012 at 8:41 AM, Sofia Ramiro <sofiaramiro@hotmail.com> wrote:
>
> Thank you very much, Maarten!
> Very useful and I can perfectly run it and adapt to my variables.
> I have been struggling during the last hours with how to adapt it when I
> want to generate missings randomly at 2 variables at the same time (and then
> 3 at the same time), etc.
> I tried thinking about finding a combination code. For example for the 2
> variables missing, if I manage to set the random generation within the
> numbers 2, 3, 4, 5, 6, 8, 10, 12, 15 and 20, these could identify uniquely
> every combination (eg: 20 = 4*5, variables 4 and 5 missing).
> Do you think would be a good approach, or do you propsoe something better?
> And if this approach, how cna I generate random numbers within these numbers
> only?
> Thank you very much!
>
> Sofia
>
> > Date: Sun, 15 Apr 2012 15:26:32 +0200
> > Subject: Re: st: Generate random missing values within a set of
> > variables
> > From: maartenlbuis@gmail.com
> > To: statalist@hsphsun2.harvard.edu
> >
> > 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/
>
> *
> *   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/
```