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 at the end of May, and its replacement, is already up and running.

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

RE: st: panel data-dropping all observations for the identifier when certain variable value is missing

From   Nick Cox <>
To   "''" <>
Subject   RE: st: panel data-dropping all observations for the identifier when certain variable value is missing
Date   Wed, 6 Jul 2011 13:06:10 +0100

I don't want to seem contrary but I think you are still misunderstanding Sabrina's question, which was, to repeat, about dropping panels, not observations as such. 

I am not sure that I understand exactly what your larger question is. Many people want to know how to understand a lot of Stata, but very quickly. In terms of literature, my best answer is to study [U] intensely, but Stata is not a spectator sport. As you say, studying many examples carefully is the best way to become fluent. 


Muhammad Anees

Dear Nick!

What I understood now from this discussion is that Sabrina needed to
drop only observations where date is less than a date in 2008 or any
specific year, and I mistakenly advised to sue _drop xvar if_ or _keep
xvar if_    pair of commands which will drop some variables instead of

I would appreciate further any idea which might help to learn Stata
and to develop a hand on selecting specific command/code/progrmming
will be most suitable to use in such short times. My guess is to use
Stata intensively for data manipulations and note what is going on in
what circumstances on exemplary data.

Lastly my intent was not misleading the list member at all.


On Wed, Jul 6, 2011 at 3:36 PM, Nick Cox <> wrote:
> The condition
> ... if offdate[_N] < 2008
> should be replaced by something like
> ... if offdate[_N] < mdy(1,1,2008)
> What you do with missings is up to you, but they would frustrate the code above.
> If any -offdate- were missing, it would be easier to go
> egen lastdate = max(offdate), by(firmid)
> drop if lastdate < mdy(1,1,2008)
> Nick
> Sabrina Helmut
> thank you very much so far. There is only one thing I did probably not describe enough. There are observations for all firms/IDs for the years 2004-2010 irrespective of whether there is data for the certain observation (. if missing). How should I rearrange your commands then? Furthermore I do not only have years but complete dates like 31dec2008.
> Thanks
> -----------------------------------------
>> Date: Wed, 6 Jul 2011 08:28:27 +0100
>> Subject: Re: st: panel data-dropping all observations for the identifier when certain variable value is missing
>> From:
>> To:
>> bysort firmid (offdate) : drop if offdate[_N] < 2008
>> bysort firmid (offdate) : gen byte islast = _n == _N
>> list firmid offdate if islast
>> See also
>> SJ-2-1 pr0004 . . . . . . . . . . Speaking Stata: How to move step by: step
>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
>> Q1/02 SJ 2(1):86--102 (no commands)
>> explains the use of the by varlist : construct to tackle
>> a variety of problems with group structure, ranging from
>> simple calculations for each of several groups to more
>> advanced manipulations that use the built-in _n and _N
>> Nick
>> On Wed, Jul 6, 2011 at 1:07 AM, Sabrina Helmut <> wrote:
>> > I have firm-year observations for the years 2003-2010. For cases where the last value for the variable <offdate>, which is a variable containing dates, is older than a
>> > date in 2008 (so date in 2007, 2006,...) then stata shall drop the firm/identifier at all, so for
>> > every year. Then, I want stata to show me for every firm/identifier the last entry for the variable <offdate>, which could then either be a date in 2008, 2009 or 2010. Which commands could help me doing this?

*   For searches and help try:

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