Statalist The Stata Listserver


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

st: RE: RE: RE: select a random obs from dataset


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: select a random obs from dataset
Date   Fri, 19 May 2006 17:01:01 +0100

To select k members from each household, the 
code should be 

gen randomvar = uniform()
bysort householdid (randomvar): gen selectionflag = _n <= k 

so that you just substitute for k whatever number
you want. 

. search _n 

yields various suggestions, including 

Example . . . . . . . . . . . . . . .  Class notes 3.0: Counting from _n to _N
        . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
        6/04    http://www.ats.ucla.edu/stat/stata/notes3/countn.htm

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        Q1/02   SJ 2(1):86-102                                   (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

Nick 
n.j.cox@durham.ac.uk 

Akihito Tokuhara
 
> The commands worked well. Thank you very much.
> I tested them on my dataset, but still not yet heard anything from my 
> colleague.
> 
> By the way, what if, say, now I want to select 3 or 4 obs from each 
> household,
> with household_ID can be selected 2 or 3 times.
> 
> I mean, I tried to change the second command line, but the 
> result is always 
> only
> one obs for each household.
> 
> Is there any keyword to look for in Help so that I can see 
> the explanation 
> in details
> of that command line, or in relation to using -- uniform() -- 
> because the 
> Help in Stata
> does not give me much info. Thank you very much in advance for any 
> guide/help.

Alex Ogan

> >This will select one person randomly per household:
> >
> >gen randomvar = uniform()
> >bysort householdid (randomvar): gen selectionflag = _n==1

Akihito Tokuhara

> >My colleague wants to know how to select a random obs with some
> >conditions from her dataset.
> >She wants to select one person randomly per household, but also with
> >condition that there is
> >no repetition of household (no household should appear twice in the
> >outcome, although the person is different).

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