Bookmark and Share

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


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

Re: st: Merging consecutive spells with the same value of Y


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Merging consecutive spells with the same value of Y
Date   Mon, 17 Jun 2013 11:29:56 +0100

Assuming no gaps

bysort ID (Start) : gen Spell =  sum(Y != Y[_n-1])
collapse (min)Start (max)End Y, by(ID Spell)

Nick
[email protected]


On 17 June 2013 11:07, Kai Huang <[email protected]> wrote:
> Dear all,
>
> I have a spell dataset as follows:
>
> ID   Start   End     Y
> 888  1981    1985    0
> 888  1985    1989    2
> 888  1989    1991    1
> 888  1991    1995    1
> 888  1995    1997    1
> 888  1997    2000    2
> 890  1985    1987    2
> 890  1987    1991    0
> 890  1991    1994    0
> 890  1994    1997    1
> I hope to merge consecutive spells with the same value of Y, so that the data becomes as follows:
> ID   Start   End     Y
> 888  1981    1985    0
> 888  1985    1989    2
> 888  1989    1997    1
> 888  1997    2000    2
> 890  1985    1987    2
> 890  1987    1994    0
> 890  1994    1997    1
>
> I use the following commands, but can only achieve my aim for ID 890:
> by ID: replace End = End[_n+1] if EconActivity==0 & EconActivity[_n+1]==0
> by ID: drop if End == End[_n-1] & Start>Start[_n-1] & EconActivity==EconActivity[_n-1]==0
> by ID: replace End = End[_n+1] if EconActivity==1 & EconActivity[_n+1]==1
> by ID: drop if End == End[_n-1] & Start>Start[_n-1] & EconActivity==EconActivity[_n-1]==1
>
> I wonder how I can merge all consecutive spells with the same value of Y, even if the number of consecutive spells are more than 2? Thank you very much in advance.
>
> Best regards,
> Kai Huang
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index