Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Creating choice indicator!

 From Joerg Luedicke <[email protected]> To [email protected] Subject Re: st: Creating choice indicator! Date Tue, 20 Mar 2012 11:05:32 -0700

I was obv on the wrong page. So if the task is to randomly select 1
out of 5 within 50% of the IDs, then this would be a possibility too:

egen tag=tag(id)
gen x=runiform() if tag
bys id: egen x1=max(x)
bys id: gen x2=runiform()
bys id (x2): gen chosen = x1>.5 & _n==1
drop tag x x1 x2

J.

On Tue, Mar 20, 2012 at 10:32 AM, Peter Grand <[email protected]> wrote:
> @ Scott: Exactly, thank you very much!
>
> @ Joerg: Sorry, if I have not been able to correctly specify my request.
>
> Again, thank you for your efforts!
>
> Best,
>
> Peter Grand
> grand(at)ihs.ac.at
> Stumpergasse 56
> 1060 Vienna
>
> ________________________________________
> Von: [email protected] [[email protected]]&quot; im Auftrag von &quot;Scott Merryman [[email protected]]
> Gesendet: Dienstag, 20. März 2012 18:27
> Bis: [email protected]
> Betreff: Re: st: Creating choice indicator!
>
> Something like this?
>
>
> clear
> set obs 5
> gen id = _n
> expand 5
> sort id
>
> bys id: gen order = _n
> gen chosen = 0
> levelsof id, local(levels)
> foreach l of local levels {
>        if runiform() < .5 {  //probability that id is chosen
>        local a = round(1 +4*runiform(),1)
>        qui replace chosen= 1 if order ==  `a' & id == `l' //1 of 5
> alternatives is chosen
>
>        }
> }
> drop order
> l
>
> Scott
>
>
> On Tue, Mar 20, 2012 at 11:51 AM, Peter Grand <[email protected]> wrote:
>> Dear Statalisters!
>>
>> How can I create a random binary variable which indicates either only one chosen alternative or none of them within an id variable, e.g. 5 choices for each individual id :
>>
>>
>> id   chosen
>> 1       0
>> 1       0
>> 1       1
>> 1       0
>> 1       0
>> 2       0
>> 2       0
>> 2       0
>> 2       0
>> 2       0
>> 3       0
>> 3       1
>> 3       0
>> 3       0
>> 3       0
>>
>>
>> Thank you very much!
>>
>> Best,
>>
>> Peter Grand
>> grand(at)ihs.ac.at
>> Stumpergasse 56
>> 1060 Vienna
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/