Statalist The Stata Listserver


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

st: cumulative product, panel data


From   "Hamilton, David" <[email protected]>
To   <[email protected]>
Subject   st: cumulative product, panel data
Date   Thu, 14 Dec 2006 11:35:44 -0500

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

The generate statement puts the first value in the first observation of
runprod.

Then the replace takes advantage of the fact that replace works its way
through the data from beginning to end, and for each observation
starting with the second, takes the prior running product, multiplies by
the current x, and puts the result in the current running product.

(Note that the final character in the "in 2/l" in the replace statement
is the letter "el", for last, not the number one.

* * *

Moreover, the function prod() is unhelpful as it only returns the total
value of the cumulative product, not the running product as t goes from
1 to 20.

My attempts to construct a loop have not been successful, either.
(Apparently, I still have much to learn.)

Your assistance and suggestions are greatly appreciated. Thanks in
advance.
-----------------------------------------
The information contained in this e-mail message, and any
attachment thereto, is confidential and may not be disclosed
without our express permission. If you are not the intended
recipient or an employee or agent responsible for delivering this
message to the intended recipient, you are hereby notified that you
have received this message in error and that any review,
dissemination, distribution or copying of this message, or any
attachment thereto, in whole or in part, is strictly prohibited. If
you have received this message in error, please immediately notify
us by telephone, fax or e-mail and delete the message and all of
its attachments. Thank you. Every effort is made to keep our
network free from viruses. You should, however, review this e-mail
message, as well as any attachment thereto, for viruses. We take no
responsibility and have no liability for any computer virus which
may be transferred via this e-mail message.

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