Statalist The Stata Listserver


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

st: Re: generating random variable


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: generating random variable
Date   Tue, 03 Apr 2007 13:29:23 -0400

This seems fairly simple:

expand 2
gen random=uniform()
bysort id bl set (random): gen byte choice=_n==_N

To make it 3 alternatives, just change expand 2 to expand 3. There is really no point in creating the random variable in this approach since all observations within a set are identical, so you could just assign it to the last observation without randomly sorting.

Michael Blasnik

----- Original Message ----- From: "Mentzakis, Emmanouil" <[email protected]>
To: <[email protected]>
Sent: Tuesday, April 03, 2007 12:26 PM
Subject: st: generating random variable



Dear all,

I need to create a variable "choice" that will take the value of 0 or 1
randomly (but in each set there has to be one occurrence of "one" and
one occurrence of "zero"). I need the various respondents (id) to have
different "choice" for the same "bl and "set", but I assume this is
taken care by the random nature of the creation of the "choice"
variable.

id bl set choice
1 1 1 1
1 1 1 0
1 1 2 0
1 1 2 1
1 1 3 0
<snip>
I would also like to create the same kind of "choice" variable but where
each "set" has three alternatives (one occurrence of "one" and two
occurrences of "zero"), i.e.
<snip>
Any help would be much appreciated.
*
*   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