Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE: How to capture record info where "something" about the record is true?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: How to capture record info where "something" about the record is true?
Date   Mon, 25 Jul 2005 21:05:56 +0100

Pleased to hear -levelsof- is useful here. 

Nick 
[email protected] 

Steichen, Thomas J.
 
> A variation on "levelsof id if <condition>" appears to 
> accomplish what I need.
> 
> It is far more efficient with respect to programmer time and 
> effort but I haven't checked whether it is cpu-cycle efficient.
 
Nick Cox
 
> > -findobs- by Sean Becketti is the most substantial 
> > work in this area that springs to mind. 
> > 
> > I can't suggest anything much simpler than 
> > 
> > gen long id = _n 
> > 
> > su id if <condition>, meanonly 
> > 
> > list id if <condition> 
> > 
> > levelsof id if <condition> 

Steichen, Thomas J.

> > > I need to efficiently capture, for example, the record id
> > > where something is true about that record.
> > > 
> > > For example, if I have the following made-up data:
> > > 
> > >   id x
> > > 
> > >   a  6
> > >   b  32
> > >   c  6
> > >   d  11
> > > 
> > > I may want to capture a record id where x = max(x).
> > > I know I can get it via:
> > > 
> > >   . egen mx = max(x)
> > >   . gen rx = 1 if x == mx
> > >   . sort rx 
> > >   . local id_info = id   (or, more accurately, id[1])
> > >   . di "ID info: `id_info'"
> > > ID info: b
> > > 
> > > But an egen, gen, and sort seems to be an expensive way to do
> > > this with a large dataset.
> > > 
> > > The above solution will, of course, only capture one record
> > > id, where x=max(x).
> > > 
> > > If I had wanted x=min(x), it would be nice if id_info
> > > contained "a c". This is doable also, but becomes even more 
> > > expensive in
> > > computing time.
> > > 
> > > Can someone propose a more efficient way or point out a
> > > built-in command that I've missed?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index