A bit more than the one line, but this should do it...
tempvar a b c
bys id (x): egen `a'=max(x)
by id: egen `b'=max(x) if x<`a'
by id: egen `c'=max(`b')
gen newvar=cond(`a'-`c'>7, `a', `c')
(I have made no attempt to deal with missing values: if you have any, use this with caution)
David
-----Original Message-----
From: Hans J. Baumgartner [mailto:hbaumgartner@diw.de]
Sent: 15 March 2005 08:50
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: gen a newvar
Thank you Nick and Ronán for your suggestions.
However, your suggestions do not work since the second largest value is
not stored in the in the second last row. So _N-1 does not work.
The variable x is strictly increasing in irregular steps. If the
difference between the largest and the second largest value of x is <=7,
newvar shall take on the value of the second largest value of x,
otherwise it shall take on the largest value of x.
In the following example newvar takes on the largest value of x for
id==1 and the secon largest value of x for id==2, since the difference
between the largest and the second largest is <=7:
id x newvar
1 5 21
1 5 21
1 10 21
1 10 21
1 21 21
1 21 21
1 21 21
2 6 70
2 6 70
2 70 70
2 70 70
2 75 70
2 75 70
Any further suggestions?
Hans
*
* 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/