Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <[email protected]> |

To |
[email protected] |

Subject |
Re: st: Taking products in loops |

Date |
Tue, 27 Mar 2012 09:13:46 +0100 |

Naturally that is only a good solution when all values are positive. Let's take the problem more cautiously. First, code that allows zero or negative values. I will assume a -time- or equivalent too. bysort event (time) : gen double product = return[1] by event : replace product = product[_n-1] * return if _n > 1 Second, what about missing values? Missing values would usually be ignored in Stata. bysort event (time) : gen double product = cond(missing(return[1]), 1, return[1]) by event : replace product = product[_n-1] * cond(missing(return), 1, return) if _n > 1 Some might object to 1 being returned as the products of all missings, which can in turn be fixed bysort event (return) : replace product = . if missing(return[1]) & missing(return[_N]) sort event time On Tue, Mar 27, 2012 at 12:26 AM, Nick Cox <[email protected]> wrote: > bysort event : gen double product = exp(sum(ln(return))) > > In addition, or rather by way of multiplying solutions, did you remember to > > . search product > > before you posted? Among various false positives are some pertinent > reminders that others have had the same problem before you: > > STB-60 dm87 . . . . . . . . . . Calculating the row product of observations > (help rprod if installed) . . . . . . . . . . . . . . . . . . P. Ryan > 3/01 pp.3--4; STB Reprints Vol 10, pp.39--41 > generates new variable whose values are the row product of > observations > > STB-51 dm71 . . . . . . . . . . . . Calculating the product of observations > (help prod if installed) . . . . . . . . . . . . . . . . . . P. Ryan > 9/99 pp.3--4; STB Reprints Vol 9, pp.45--48 > extension to egen for producing the product of observations > > Nick > > On Mon, Mar 26, 2012 at 11:44 PM, Sandy Y. Zhu <[email protected]> wrote: > >> I was just wondering if you would know how to take products in Stata. >> >> Here's how my data looks like: >> >> Event Return >> 1 1.1 >> 1 1.01 >> 1 0.99 >> 1 1.23 >> 1 1.1 >> 1 1.34 >> 1 0.88 >> 2 0.99 >> 2 0.87 >> 2 1.1 >> 2 1.004 >> 3 1.02 >> 3 1.09 >> 3 0.98 >> 3 0.98 >> 3 1.09 >> 3 1.09 >> ..... ............ >> >> For each event, I would like to multiply all the numbers under the >> column "return" for that event together. At the end of the program, I >> would like to output a column with all the "cumulative returns" for >> each event. Since each of my events containa different number of >> observations (or returns), I'm wondering if there's a command that can >> do that in Stata? * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Taking products in loops***From:*"Sandy Y. Zhu" <[email protected]>

**Re: st: Taking products in loops***From:*Nick Cox <[email protected]>

- Prev by Date:
**st: nlsur quaids** - Next by Date:
**st: Filling in Bar Graphs with different texture/patterns/gradients** - Previous by thread:
**Re: st: Taking products in loops** - Next by thread:
**st: Standard deviation with pweight or fweight** - Index(es):