Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[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 <demonsecret@hotmail.com.hk>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index