# Re: st: Dichotomizing a Variable

 From "Alan Neustadtl" To statalist@hsphsun2.harvard.edu Subject Re: st: Dichotomizing a Variable Date Fri, 7 Apr 2006 11:03:59 -0400

```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 <drreg@earthlink.net> 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/
```