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: consecutive years


From   "Biljana Dlab" <[email protected]>
To   <[email protected]>
Subject   st: consecutive years
Date   Mon, 18 Oct 2010 14:02:10 +0200

Dear Stata list 

I need to identify the run of consecutive years and the shortest string
should be 4.

I did it like this:

. tsset  firm  year

. gen run=.
. by  firm: replace run = cond(L.run==., 1, L.run + 1)
. by  firm: egen maxrun = max(run)
. drop if maxrun<4

. by  firm: drop if run==3 & run[_n+1]!=4
. by  firm: drop if run==2 & run[_n+1]!=3
. by  firm: drop if run==1 & run[_n+1]!=2

However this code results that for some firms I have two (or more)
strings with consecutive years - both these strings are 4 or more
consecutive years long. 
I would like to indentify such a cases and I would like to keep both
(all) string, but under modified "firm" (i.e. original firm number + 1)

Does someone knows how should I modify my code?


Please find below example, i.e. result when applied the code from above:

firm	year	corr		run13	maxrun13
31	1991	-0.5604	1	6
31	1992	-0.5604	2	6
31	1993	-0.5604	3	6
31	1994	-0.5604	4	6
31	1995	-0.5604	5	6
31	2000	-0.5604	1	6
31	2001	-0.5604	2	6
31	2002	-0.5604	3	6
31	2003	-0.5604	4	6
31	2004	-0.5604	5	6
31	2005	-0.5604	6	6

Thank you.

Best regards,

Biljana

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index