# Re: st: How to generate a cumulative sum

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: How to generate a cumulative sum Date Fri, 26 Oct 2007 10:05:34 -0400

```But Nick Cox also gave you a solution with depreciation explicit:

sort firm year
tsset firm year
gen K = bookvalue
by firm: replace K = 0.95 * L.K + I if _n > 1

in both of our solutions, you can go very wrong if you define capital
stock from book value in the wrong period(s).  Nick's solution assumes
you have bookvalue defined only in the first period observed for each
firm; I assume you have it defined wherever you don't have a computed
value for lagged capital stock (including when data skips a period or
more).

On 10/26/07, Austin Nichols <austinnichols@gmail.com> wrote:
> Like so:
>
> loc d 0.05
> webuse grunfeld, clear
> g k0=int(uniform()*1000) if year==1935
> g k=.
> bys c (y): replace k=cond(l.k<.,l.k*(1-`d'),k0)+i
> li com year k0 k inv in 1/10, noo sepby(com)
>
> On 10/26/07, Scott Merryman <scott.merryman@gmail.com> wrote:
> > There needs to be a space between c and (y) and d has not been defined.
>
*
*   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/
```