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


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

Re: st: onespell


From   "Fabian Schönenberger" <sch.f@gmx.ch>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: onespell
Date   Tue, 04 Sep 2012 15:45:22 +0200

Many thanks, Nick. With those commands I am able to resolve the sample from gaps, and keep the longest and most recent spells. 

FS

-------- Original-Nachricht --------
> Datum: Tue, 4 Sep 2012 11:21:08 +0100
> Von: Nick Cox <njcoxstata@gmail.com>
> An: statalist@hsphsun2.harvard.edu
> Betreff: Re: st: onespell

> -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/
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index