Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: how to (elegantly) take max of next five observations


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: how to (elegantly) take max of next five observations
Date   Sun, 10 Dec 2006 18:33:22 -0000

It wouldn't be -egen, rowmax()- even if that obeyed your 
syntax, as this is a column operation. 

I don't think this is (much (more)) elegant, 
but it's another way:

webuse grunfeld, clear
gen maxin5 = mvalue
qui forval i = 1/5 {
	replace maxin5 = max(maxin5, F`i'.mvalue)
}
l company mvalue maxin5

I wouldn't call this the maximum of the next five, 
but that's a boundary call. Also, this behaves
differently at the end of the panel. Finally, time series
operators and expressions based on _n won't agree
if there are gaps. The operators are always preferable. 

Nick 
[email protected] 

Caleb Southworth
 
> I'm looking for a quick way to check the next five values in panel
> dataset.
> 
> Data are
> . tsset
>        panel variable:  id (strongly balanced)
>         time variable:  year, 1888 to 2005, but with gaps
> 
> This works:
> 
> bysort id (year): gen for5=max(mem, mem[_n+1], mem[_n+2], mem[_n+3],
> mem[_n+4], mem[_n+5])
> 
> but involves the messy indexing. I would like something like this:
> 
> . egen for5=rowmax(F(1/5).mem_data)
> time-series operators not allowed
> r(101);
> 

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