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.

Re: st: onespell

 From Nick Cox To statalist@hsphsun2.harvard.edu 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

Then

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?
http://www.stata.com/support/faqs/data/panel.html

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.

Nick

On Tue, Sep 4, 2012 at 10:48 AM, "Fabian Schönenberger" <sch.f@gmx.ch> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

• References: