I am not sure that I understand the reference to _N here.
With a few guesses it sounds as if you want something
like this
bysort id (x) : gen newvar = cond(x[_N] - x[_N-1] <= 7, x[_N-1], x[_N])
This will not work with missing values, so you might want
something more like
gen byte present = !mi(x)
bysort present id (x) : gen newvar = <same stuff> if present
bysort id (newvar) : replace newvar = newvar[1] if mi(x)
Nick
n.j.cox@durham.ac.uk
Hans J. Baumgartner
> My data looks like this:
>
> id x
> 1 5
> 1 5
> 1 10
> 1 10
> 1 21
> 1 21
> 1 21
> 2 ...
>
> I want to generate a newvar with the maximum value of x ,
> which is _N.
> But if the difference between the largest and the second
> largest value
> is <=7, newvar should take the value of the second largest value of x.
*
* 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/