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

Re: st: RE: gen a newvar


From   "Hans J. Baumgartner" <[email protected]>
To   [email protected]
Subject   Re: st: RE: gen a newvar
Date   Tue, 15 Mar 2005 09:49:35 +0100

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


Nick Cox schrieb:


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/
--
Hans J. Baumgartner DIW Berlin
German Institute for Economic Research
Dept. Public Economics
K�nigin-Luise-Str. 5; 14195 Berlin; Germany
Tel.: +49/30/89789-307
Fax.: +49/30/89789-114
http://www.diw.de
http://www.hansbaumgartner.de


*
*   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