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

# Re: st: Continues panel data

 From Nick Cox <[email protected]> To "[email protected]" <[email protected]> Subject Re: st: Continues panel data Date Tue, 15 Oct 2013 16:35:02 +0100

```Short answer is Yes. Note that the FAQ I cited is relevant.

I don't know what's unclear, but for this code

(1) bysort firmid (year) : gen diff = cond(_n == 1, 1, year - year[_n-1])
(2) bysort firmid (diff) : drop if diff[_N] > 1

the key points are

(1)

Sort by identifier and year.

Then within each panel, each year should be 1 greater than the
previous year. That difference is

year - year[_n-1]

However, there is a small problem. The first year for each panel has
no previous year, so Stata will return missing. We fix that by
spelling out

cond(_n == 1, 1, year - year[_n-1])

i.e. for the first observation in each panel, just return 1; otherwise
return the difference. Doing this under -by firmid:- ensures that the
observation number _n is interpreted _within panels_.

(2)

So we have a variable -diff-. If everything is OK, it is always 1. To
find out, you could

sort firmid diff

and then the _highest_ value of -diff- will be sorted to the end of
each panel. But we can check and -drop- all in one with

bysort firmid (diff) : drop if diff[_N] > 1

As we are (again) working under -by firmid:- _N is interpreted within
panels as the last observation in each panel. If any value of -diff-
is above 1, we -drop- the entire panel.

I didn't test this....

Nick
[email protected]

On 15 October 2013 16:19, Ozgur Ozdemir <[email protected]> wrote:
> Hi Nick,
>
> Data might start in any year but needs to be continuous until the last year. so I believe I sohuld use
>
>> bysort firmid (year) : gen diff = cond(_n == 1, 1, year - year[_n-1])
>
> am I right.
>
> However, i did not get the difference between above and below:
>
>> bysort firmid (diff) : drop if diff[_N]> 1
>
> Can you clarify please ?
>
> kind regards
> Ozgur Ozdemir
>
>
>
> ----------------------------------------
>> Date: Tue, 15 Oct 2013 15:39:20 +0100
>> Subject: Re: st: Continues panel data
>> From: [email protected]
>> To: [email protected]
>>
>> There are various ways to do this, depending on your details.
>>
>> The simplest is where an ideal panel includes say 10 annual values in which case
>>
>> bysort firmid : drop if _N < 10
>>
>> removes incomplete panels. Modify for "any value of 10".
>>
>> Another is where panels may start and end in different years but gaps
>> are not acceptable.
>>
>> bysort firmid (year) : gen diff = cond(_n == 1, 1, year - year[_n-1])
>>
>> calculates a value that should always be 1 and
>>
>> bysort firmid (diff) : drop if diff[_N]> 1
>>
>> will drop panels with any values of 2 or more.
>>
>> If neither matches your exact situation, please spell out what it is.
>>
>>
>> http://www.stata.com/support/faqs/data-management/identifying-runs-of-consecutive-observations/
>>
>>
>> Nick
>> [email protected]
>>
>>
>> On 15 October 2013 13:46, Ozgur Ozdemir <[email protected]> wrote:
>>> Hi,
>>>
>>> I have a panel data (firm Id, year, sales etc), but would like to remove if there are any gaps for a given firm. e.g If a firm does have a record in 2000, and 2002, but not in 2001, I would like to remove it,.
>>>
>>> Is there any way of doing this in Stata please?
>>>
>>> thanks, Ozgur
>>> *
>>> * 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/
> *
> *   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/
```