clear
sysuse auto
set seed 040706
su rep78, d
g select=uniform() if rep78==r(p50)
g d=(rep78<r(p50)) if !mi(rep78)
sort select
su select
replace d=1 if _n<(r(N)+1)/2
replace d=round(uniform()) if _n==(r(N)+1)/2
tab rep78 d
guarantees an even split of median values, except when there are an
odd number of obs with the median value, in which case it randomly
picks one of the two "most even" splits possible.
Just out of curiosity, why would you want to do that?
On 4/7/06, Alan Neustadtl <[email protected]> wrote:
> There are probably more efficient/clever ways of doing this, but you
> can generate a random number from a uniform distribution and use this
> to randomly assign cases to either the 0 or 1 condition. Of course,
> there is no guarantee that exactly half of the cases will be 0 an
> dhalf 1, but at least they are randomly assigned this state. The
> following example shows how this works using the auto data.
>
> sysuse auto
> tabstat rep78, statistics(median)
>
> set seed 040706
> generate select=uniform()
>
> generate dichot=.
> replace dichot=0 if rep78<3
> replace dichot=1 if rep78>3 & rep78 <.
> replace dichot=0 if rep78==3 & select <.5
> replace dichot=1 if rep78==3 & select >.5
> tab rep78 dichot
> drop select
>
> Best,
> Alan
>
> On 4/7/06, Reg Jordan <[email protected]> wrote:
> > Suppose I have a variable, abc that consists of: 1 2 3 4 5 5 6 7 8 9. I want
> > to dichotomize the variable at the median.
> >
> > generate abcnew = 0
> > replace abcnew = 1 if abc > 5
> >
> > What statement do I write that assigns one of the 5s a 0 and the other 5 a 1
> > in abcnew?
> >
> > Thanks for the assistance.
> >
> > reg
> >
> > *
> > * 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/
> >
>
> *
> * 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/
>
*
* 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/