Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: How to randomly pick one observation from all?


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: How to randomly pick one observation from all?
Date   Wed, 12 Jun 2002 09:00:50 +0000

Yanjun Bao  wrote
> We have say, by one id, four observations under a variable.  We want STATA
> to randomly pick one observation each time it is asked.  We want to use the
> randomly picked observation for each id in regression.


You can sort by a random variable within each person and than pick the first 
observation or each person. If "id" is the identifier for observation the 
following should do it:

.. set seed 731           /*  Change seed to whatever you want */
.. gen r = uniform()      
.. by id (r), sort: gen select = _n==1
.. regress yvar xvars if select


hope this helps
uli

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