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, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: onespell

From   Nick Cox <>
Subject   Re: st: onespell
Date   Tue, 4 Sep 2012 11:21:08 +0100

-onespell- uses -tsspell- (SSC), which you will have downloaded. The
use of -tsfill- within -onespell- is not optional, so you can fall
back on -tsspell- and some data management.

Let's suppose that the time variable is -fyear- as in your question
and that you have panel identifier -id- and have

tsset id fyear


tsspell, f(L.fyear == .)

identifies spells of contiguous observations. See the help or

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?

The longest such spell can be identified by calculating in turn the
length of each spell

egen length = max(_seq), by(id _spell)

and then the longest for each panel

egen maxlength = max(length), by(id)

so that

keep if length == maxlength

would keep the longest spell. However, two or more spells could be of
the same length for any -id-. If so, all are retained. You would need
another rule for selecting which. For example

bysort id (_spell) : keep if _spell == _spell[1]

keeps the first such spell, and

bysort id (_spell) : keep if _spell == _spell[_N]

keeps the last.

Warning: all code untested.


On Tue, Sep 4, 2012 at 10:48 AM, "Fabian Schönenberger" <> wrote:
> Dear Statalist
> I am using onespell, SSC stata modul from Christopher F. Baum, for panel data. I write:
> onespell fyear,saving(test)
> Observations removed: -18332
> file test.dta saved
> fyear is a time variable (annual). For certain id, fyear has gaps if for instance the company stopped generating annual reports and started again after some years passed. I expected onespell to keep only the longest contiguous spells of fyear for each id, and other observations will be deleted from the file. But onespell adds new rows filling the gaps of fyear. In my case, onespell adds 18332 new observations (all observations except from fyear are missing).
> How can I keep the longest contiguous spells for fyear for each id and drop the other observations?

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index