Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: using loops in collapse command |

Date |
Mon, 2 Apr 2012 19:38:53 +0100 |

You don't need -collapse-. But you could look at -lastnm-. Or drop if mi(value1) bysort id year : gen double product = exp(sum(ln(value1))) by id year : keep if _n == _N By the way, bysort id year (date) is overkill if you only want the last value. The final product doesn't depend on the order of its arguments. Nick On Mon, Apr 2, 2012 at 7:30 PM, Jocelyn Michael <michaeljocelyn31@gmail.com> wrote: > Thank you Nick. That worked! Thanks also for the tip about working > around missing values. > > As a follow up: How do I collapse the different numbers for the years > leaving only one id for one year with one value1? > > When I do collapse, it takes the mean but thats not what I want. > > Thanks > > J. > > On Mon, Apr 2, 2012 at 1:37 PM, Nick Cox <njcoxstata@gmail.com> wrote: >> %ty is a format. It won't fit well into this framework. If you are >> saying that you want separate products for each individual and year >> that would be at root something like >> >> bysort id year (date) : gen double product = exp(sum(ln(value1))) >> >> Evidently you are getting rid of missing values. The thread cited >> earlier showed a way of working round them. >> >> Nick >> >> On Mon, Apr 2, 2012 at 6:29 PM, Jocelyn Michael >> <michaeljocelyn31@gmail.com> wrote: >>> Thank you to all. I have tried the suggested approaches and I am >>> almost there. I guess the part I omitted in my question is that I have >>> multiple years. So the solutions you have suggested resolve most of >>> the issue - but I am still experiencing some problems. >>> >>> here is what I have done so far: >>> >>> by id (date), sort:drop if _n==sum(mi(value1)) >>> sort id date >>> gen ym = yofd(date) >>> format ym %ty >>> >>> drop if value1==. >>> foreach %ty bysort id gen double product = exp(sum(ln(value1))) >>> >>> It works properly if I don't have the second restriction of trying to >>> sort into years i.e. the (foreach %ty) part. >>> >>> Thanks for your assistance. >>> >>> J. >>> >>> On Mon, Apr 2, 2012 at 11:10 AM, Nick Cox <njcoxstata@gmail.com> wrote: >>>> The geometric mean is only usefully defined for strictly positive >>>> values. If that is the case, then the solution at >>>> >>>> http://www.stata.com/statalist/archive/2012-03/msg01142.html >>>> >>>> is equivalent to this, requires no installed extras and is more direct. >>>> >>>> Nick >>>> >>>> On Mon, Apr 2, 2012 at 4:03 PM, Alberto Dorantes >>>> <alberto.dorantes@finanzastec.net> wrote: >>>>> Hi Michael. >>>>> You can install the egenmore function first using findit egenmore. >>>>> >>>>> Then, you can type: >>>>> >>>>> egen gm=gmean(value1), by(id) >>>>> egen nn=count(value1), by(id) >>>>> gen prod=gm^(nn) >>>>> >>>>> I hope this help... >>>>> >>>>> Alberto. >>>>> >>>>> 2012/4/2 Jocelyn Michael <michaeljocelyn31@gmail.com>: >>>>>> I am a new stata user and have been trying to write a loop to accomplish a task. >>>>>> >>>>>> My data looks like this: >>>>>> >>>>>> >>>>>> id Year Value 1 >>>>>> A Jan1991 0.92 >>>>>> A Feb1991 0.93 >>>>>> A Mar1991 0.24 >>>>>> A APr1991 0.83 >>>>>> .. >>>>>> .. >>>>>> B Jan1991 0.67 >>>>>> B Feb1991 0.45 >>>>>> .. >>>>>> >>>>>> What I am trying to achieve is to write a loop that multiplies value1 >>>>>> for each id and returns one number for each year - sort of like a >>>>>> collapse command but instead of the sum, I want the product. That is >>>>>> for A for 1991, (0.92*0.93*0.24*0.83). >>>>>> >> * * 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/

**Follow-Ups**:**Re: st: using loops in collapse command***From:*Jocelyn Michael <michaeljocelyn31@gmail.com>

**References**:**st: using loops in collapse command***From:*Jocelyn Michael <michaeljocelyn31@gmail.com>

**Re: st: using loops in collapse command***From:*Alberto Dorantes <alberto.dorantes@finanzastec.net>

**Re: st: using loops in collapse command***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: using loops in collapse command***From:*Jocelyn Michael <michaeljocelyn31@gmail.com>

**Re: st: using loops in collapse command***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: using loops in collapse command***From:*Jocelyn Michael <michaeljocelyn31@gmail.com>

- Prev by Date:
**Re: st: using loops in collapse command** - Next by Date:
**Re: st:Normality and Granger Causality in Panel data** - Previous by thread:
**Re: st: using loops in collapse command** - Next by thread:
**Re: st: using loops in collapse command** - Index(es):