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]

st: Merging consecutive spells with the same value of Y


From   Kai Huang <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Merging consecutive spells with the same value of Y
Date   Mon, 17 Jun 2013 10:07:36 +0000

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/


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