Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: RE: RE: listing all observations of particular id's

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: RE: RE: listing all observations of particular id's Date Sun, 29 Aug 2010 19:34:14 +0100

```It's like this.

1. by A: egen other_anyB1 = max(B)

Stata sees the -by- first, and asks: Are observations -sort-ed by A? If the answer is No, an error is generated.

1'. bysort A: egen other_anyB1 = max(B)

Stata sees the -by- first, and asks: Are observations -sort-ed by A? If the answer is No, the -sort-ing is done, and no error is generated. Stata proceeds to the -egen- call.

2. egen anyB1 = max(B), by(A)

This is in fact an old syntax which is no longer documented, but I remember it and still use it. There are some further subtleties:

* -by()- isn't even an undocumented option of -egen-. It _may be_ an undocumented function of whatever -egen- function is being called.

shows that -by()- is allowed as an option in this case, which is why it works. And the -sort-ing is done for you within the _gmax program.

* As -egen- is -sortpreserve, you get your old -sort- order back. So any changes in -sort- order done by -egen, max()- are undone.

Note: When you call -egen-'s <foobar> function, Stata needs to find an _g<foobar> program on your -adopath-.

Nick
n.j.cox@durham.ac.uk

Martin Weiss

On the occasion of this thread and NJC`s solution: Why is it that Stata
requires no -sort-ing in Nick`s solution, but does require it if I prefix
the -by-? (For -max-, -sort-ing should not matter...)

***********
clear*
inp byte(A B)
1 0
1 1
2 0
1 0
2 1
3 0
2 0
3 0
2 1
end
egen anyB1 = max(B) , by(A)
by A: egen other_anyB1 = max(B)
***********

Nick Cox

This is an FAQ.

egen anyB1 = max(B) , by(id)
l if anyB1

The rationale is discussed at tedious length in

How do I create a variable recording whether any members of a group (or all
members of a group) possess some characteristic?

<http://www.stata.com/support/faqs/data/anyall.html>

Nick
n.j.cox@durham.ac.uk

Abhimanyu Arora

The basic simplified problem is as follows.
2 variables A (id) and B(0/1)
I would like to list all observations of all A's for which there is at
least one B=1.
-list- lists only the particular 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/
```