Statalist The Stata Listserver


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

st: Re: cumulative product, panel data


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: cumulative product, panel data
Date   Thu, 14 Dec 2006 12:48:40 -0500

There are several ways you could do this. I think you discarded the exp(sum(ln(x))) aproach too quickly. You could do this:

bys id (t): gen sumprod=exp(sum(ln(x)))
by id (t): gen sumzero=sum(x==0)
replace sumprod=0 if sumzero>0
drop sumzero

You could also have used the approach of the thread you found unhelpful by realizing that, although you may not use -in 2/l- after a -by-, you can use -if _n>1-

Michael Blasnik

----- Original Message ----- From: "Hamilton, David" <[email protected]>
To: <[email protected]>
Sent: Thursday, December 14, 2006 11:35 AM
Subject: st: cumulative product, panel data



I'm having difficultly with what seems a straightforward programming
exercise. I am trying to calculate the cumulative product for a variable
for panel data set.

My data basically consists of the following 3 variables: id, t, x.

id runs from 1 to 1,000; t (time interval) runs from 1 to 20; and x is a
ratio for each t (and id).

I am trying to calculate the cumulative product of x for each id across
time interval 1/20 in a new variable (prodx, say).

x does sometimes take the value zero (so exp(sum(ln(x))) isn't an
option).

I found the following thread unhelpful as "in" cannot be used with "by":

* * *
Assuming your original variable is x:

        . generate runprod = x in 1
        . replace runprod = runprod[_n-1]*x in 2/l

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