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

Re: st: RE: Is there a statacommand for a Historical Maximum? & Howto cumulate series?


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: st: RE: Is there a statacommand for a Historical Maximum? & Howto cumulate series?
Date   Mon, 26 Sep 2005 13:33:49 +0100

This works, but no loop is necessary. To repeat Michael
Blasnik's earlier solution:

gen cummax=x in 1
replace cummax=max(x,cummax[_n-1]) in 2/l

-egen, record()-, which is more general than this,
uses the same basic idea.

The principle, which Scott knows very well but momentarily
forgot, is that

*** -generate- and -replace- use the current sort order ***

So, if a calculation requires the result of the same
calculation for the previous observation, that is no
problem. Typically Stata with code like this makes it
available just in time, but that's time enough.

This isn't documented very prominently, but it is true.

Nick
[email protected]

Scott Merryman
Yes.

1. -egen-
2. -gen-

For example:

sysuse gnp96.dta,clear
count
gen max = .
qui {
forv i = 1/`=r(N)' {
	egen temp = max(gnp) in 1/`i'
	replace max = temp in `i'
	drop temp
	}
}
gen cumul_gnp = sum(gnp)


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