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: drop observations within groups under certain conditions


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: drop observations within groups under certain conditions
Date   Fri, 28 Jun 2013 13:54:55 +0100

Stata makes this kind of calculation very easy and it's very useful
once beyond absolute beginner level to be comfortable about how to do
it.

The starting point has to be that you are interested in counting
incidences of positive earnings

earnings > 0

noting that we must exclude missings because missings are treated as
large positive numbers:

earnings > 0 & earnings < .

So we could form a variable

gen ispos = earnings > 0 & earnings < .

-ispos- will be 1 or 0 depending on whether the condition is true or false.

Then we want to add those values up for each manager, which we could do by

bysort manager : replace ispos = sum(ispos)
by manager : replace ispos = ispos[_N]

In fact there is a much easier shortcut, to use -egen-:

egen npositive = total(earnings > 0 & earnings < .) , by(manager)

drop if npositive < 2

Note that for -egen- we can use an _expression_ as argument to
-total()-, so we don't need to create an indicator first.

Nick
njcoxstata@gmail.com

On 28 June 2013 13:42, Nahla Betelmal <nahlaib@gmail.com> [edited]

> my data in focus represent three variables : manager ID (unique),
> tenure period (which varies among managers) and earnings (which could
> be positive, zero, negative, or missing).
>
> I want to keep only the managers who have positive earnings at least
> twice (at least for two years). To be more clear I want to keep all
> the observations for the manager who has positive earnings at least
> twice, regardless of whether in the other years he has non-positive earnings.
>
> In other words I want to drop managers who have no positive earnings
> at all, or positive earnings once during his tenure period.

[..]

> year  manager_ID earnings
> 1995   A123            + 12
> 1996   A123            + 4
> 1997   A123               0
> 1998   A123              -3
> 1999   A123               .
>
> 1997   B567            + 5
> 1998   B567             -2
> 1999   B567              .
>
*
*   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/


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