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

st: RE: gen a newvar


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: gen a newvar
Date   Mon, 14 Mar 2005 17:26:57 -0000

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 
[email protected] 

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/



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