Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: AW: st: RE: creating expanded records with indicator variables, where different ID's reside in the same rows


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: AW: st: RE: creating expanded records with indicator variables, where different ID's reside in the same rows
Date   Wed, 21 Oct 2009 21:54:53 +0200

<> 


I had a solution involving -stack- that was a _couple_ of lines longer than
the winning proposal by Nick...



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Michael
McCulloch
Gesendet: Mittwoch, 21. Oktober 2009 21:01
An: [email protected]
Betreff: Re: AW: st: RE: creating expanded records with indicator variables,
where different ID's reside in the same rows

Thanks Martin. Nevertheless, I learned something useful with -stack-.

On Oct 21, 2009, at 11:55 AM, Martin Weiss wrote:

>
> <>
>
>
> " the lingering bug is what to do about records where both samples A  
> and
> B"
>
>
> Stick to Nick`s solution, I was outgunned on this one :-)
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Michael
> McCulloch
> Gesendet: Mittwoch, 21. Oktober 2009 20:07
> An: [email protected]
> Betreff: Re: st: RE: creating expanded records with indicator  
> variables,
> where different ID's reside in the same rows
>
> Thanks to Nick and Jeph and Martin. Here's a summary:
> 1. Nick's example works very well.
> 2. Jeph's example not allowing the -keep- statement, probably because
> I'm using v10.
> 3. I created a partial based on Martin's recommendation of -stack-;
> the lingering bug is what to do about records where both samples A and
> B are true for the same id, as in ID==11?
>
> * My solution
> *****
> webuse stackxmpl, clear
> drop c d
> set obs 3
> replace a=11 in 3
> replace b=11 in 3
> list
> stack a a  b b, into(id) clear wide
> sort _stack
> drop if a==.&b==.
> replace a=1 if a!=.
> replace b=1 if b!=.
> replace a=0 if a==.
> replace b=0 if b==.
> sort id
> list id a b
> *****
>
> * Nick's example
> *****
> webuse stackxmpl, clear
> drop c d
> set obs 3
> replace a=11 in 3
> replace b=11 in 3
> list
> gen long id = _n
> expand 2 if a != b
> bysort id : replace id = cond(_n == 1, a, b)
> replace a = a == id
> replace b = b == id
> l id a b
> *****
>
>
>
> On Oct 21, 2009, at 10:52 AM, Nick Cox wrote:
>
>> OK. Try this:
>>
>> input a b
>> 5 5
>> 6 11
>> end
>> gen long id = _n
>> expand 2 if a != b
>> bysort id : replace id = cond(_n == 1, a, b)
>> replace a = a == id
>> replace b = b == id
>> l
>>
>>
>> Nick
>> [email protected]
>>
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Michael
>> McCulloch
>> Sent: 21 October 2009 18:21
>> To: [email protected]
>> Subject: Re: st: RE: creating expanded records with indicator
>> variables,
>> where different ID's reside in the same rows
>>
>> Sorry Nick, yes you're correct, it should be:
>> id	A	B
>> 5	1	1
>> 6	1	0
>> 11	0	1
>> Michael
>>
>> On Oct 21, 2009, at 10:18 AM, Nick Cox wrote:
>>
>>> Sorry, but I can't even follow your first two steps. Why isn't the
>>> second observation 6 1 0?
>>>
>>> Nick
>>> [email protected]
>>>
>>> Michael McCulloch
>>>
>>> I have a data structure that has been gathered as follows, where  
>>> each
>>> cell contains the subject ID if A is true, and similarly for B.
>>> Therefore, for subject 5, I have A=yes and B=yes, whereas for
>>> subjects
>>> 6 and 11, only B=yes.
>>>
>>> A	B
>>> 5	5
>>> 6	11
>>>
>>> I'd like to transform the data structure such that each row would
>>> have
>>> ID, and indicator variables for A and B.
>>> id	A	B
>>> 5	1	1
>>> 6	0	1
>>> 11	0	1
>>>
>>> I've tried an approach like this, where I create indicator variables
>>> but A and B, but am not sure how to "expand" from  the data  
>>> structure
>>> where different ID's reside in the same rows.
>>> 	. gen id1=a
>>> 	. gen id2=b
>>> 	. gen aa=1 if a!=.
>>> 	. gen bb=1 if b!=.
>>>
>>> It also looks like -reshape- is not suitable because different ID's
>>> reside in the same rows.
>>> Any advice would be appreciated.
>>
>> *
>> *   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/
>
>
>
> Michael McCulloch
> Pine Street Foundation
> 124 Pine Street
> San Anselmo, CA 94960-2674
> tel:	415-407-1357
> fax: 	206-338-2391
> [email protected]
>
>
>
>
>
>
>
> *
> *   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/



Michael McCulloch
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391
[email protected]









*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index