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/