Statalist


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

Re: st: random sample selection


From   Svend Juul <SJ@SOCI.AU.DK>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: random sample selection
Date   Tue, 1 Apr 2008 15:23:10 +0200

Raoul wrote:

I am trying to get a random sample of 40 subjects from a dataset of a
few hundred subject. This is what I did:

.set seed 12345
.gen random = uniform()
.sort random, stable
.drop if _n>40

For various reasons, it might be that some subjects in the sample are
not suitable for selection and I want to exclude them from the sample
and from the overall dataset.

So I selected another sample, however, when I did that other subjects
were selected as well.

.drop if id == 890800  //subject that wasn't suitable dropped
.set seed 12345
.gen random = uniform()
.sort random, stable
.drop if _n>40

What I want is to keep the original 39 subjects as in the old sample
in  and just replace the subject that wasn't suitable. Any suggestions
would be appreciated.

===================================================================

The sequence of events is important; the unwanted observation should 
be dropped AFTER the random number generation. Unless you are so 
unlucky that the 41st subject is unwanted, too, this should work:

. set seed 12345
. gen random = uniform()
. sort random, stable
. drop if _n>41
. drop if id == 890800  //subject that wasn't suitable dropped

Hope this helps
Svend

__________________________________________

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone:  +45 8942 6090
Home:   +45 8693 7796
Email:  sj@soci.au.dk
__________________________________________ 


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index