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.

# st: RE: RE: Dropping non-consecutive observations in a panel

 From "Nick Cox" To Subject st: RE: RE: Dropping non-consecutive observations in a panel Date Thu, 1 Jul 2010 11:39:31 +0100

```See

FAQ     . . . . . . Identifying runs of consecutive observations in
panel data
. . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and V.
Wiggins
8/02    How do I identify runs of consecutive observations
in panel data?
http://www.stata.com/support/faqs/data/panel.html

for a discussion of this area. It follows that

sort id time
egen max_gap = max(time - time[_n-1]), by(id)
drop if max_gap > 1

gives a solution, regardless of whether -tsset- or -xtset- has been
applied.

Nick
n.j.cox@durham.ac.uk

Martin Weiss

Btw, 11.1 is out, so you should -update all-

***********
clear*

inp byte(id	time	first_time	last_time	a	b
c
)
1	1	1	4	  4	4	0
1	2	1  4  	4	4	0
1	3	1  4  4	4	0
1	4	1 4   4	4	0
2	2	2	4	  3	3	0
2	3	2	4	  3	3	0
2	4	2	4	  3	3	0
3	1	1	4	  3	4	1
3	3	1	4	  3	4	1
3	4	1	4	  3	4	1
end

xtset id time
bys id: gen diff=D.time
by id: egen mycount=count(diff)
l, sepby(id)

by id: drop if mycount != _N-1
drop mycount diff
l, sepby(id)
***********

Lynnae Ruberg

I'm using Stata/SE 11.0 for Windows to analyze a panel data set. I
-xtset-
time and id for the panel. I would like to use only the panels that have
unbroken observations across time - i.e. if id 1 was observed in time
periods 1, 2, and 4, I don't want to use it, but if it was observed in
2, 3,
and 4, then I do.

When I run -xtdescribe- I see that 98% of my data have an unbroken
pattern
(like the latter example above). Is there a straightforward way of
telling
Stata to only use these & drop (or simply ignore) the rest?
Alternatively,
is there a way to browse the data according to the pattern it has in
-xtdescribe-?

Here's the work-around I've come up with for identifying skipped ids,
but
I'd love to know if there's any easier way of approaching it.

sort id(time)
by id: gen first_time=time[1]
by id: gen last_time=time[_N]
by id: egen a=count(time)
gen b=(last_time-first_time)+1
gen c=b-a

id	time	first_time	last_time	a	b	c
1	1	1     	4	      4	4	0
1	2	1	      4      	4	4	0
1	3	1	      4	      4	4	0
1	4	1      	4	      4	4	0
2	2	2     	4	      3	3	0
2	3	2     	4	      3	3	0
2	4	2     	4	      3	3	0
3	1	1     	4	      3	4	1
3	3	1     	4	      3	4	1
3	4	1     	4	      3	4	1

In id==3, time period 2 is skipped; this is "caught" by the different
between the count of time periods per id (variable a) and the total
number
of time periods that id is observed - if an id isn't skipped, the
difference
will be 0. I'd use -if c==0- in my analysis. Doing it this way
identifies
97% of the data as unbroken (so it's not producing the same result as
-xtdescribe-, which I can't figure out).

If there is an easier/more accurate way of doing this, I'd love to know
how.

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