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 |
Ryan Kessler <ryan.kessler.stata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: How to Calculate A Variable of Maximum Values up to each of the 114 periods in my sample? |

Date |
Fri, 14 Dec 2012 12:12:45 -0500 |

How about something like this: input x 10 100 0 400 30 1000 60 end gen runmax = x replace runmax = runmax[_n-1] if runmax[_n-1] > runmax[_n] /// & !missing(runmax[_n-1]) Ryan On Fri, Dec 14, 2012 at 11:53 AM, Yuval Arbel <yuval.arbel@gmail.com> wrote: > Dear Statalisters, > > I am trying to generate a variable, which will give me the maximum > value of another variable (reduct_per) up to each of the 114 periods > in my sample. > > Note that the function -max- works only with -egen-, namely it creates > a constant maximum, and not a running maximum.. > > I therefore wrote the following set of commands: > > by appt: egen y=max(reduct_per) > by appt: gen max=reduct_per if time_index==0 > by appt: replace max=reduct_per if reduct_per==y > by appt: replace max=reduct_per[_n-1] if reduct_per<=reduct_per[_n-1] > & max==. & time_index==1 > by appt: replace max=reduct_per if max==. & time_index==1 > > by appt: replace max=max[_n-1] if reduct_per<=max[_n-1] & max==. & > time_index==2 > by appt: replace max=reduct_per if max==. & time_index==2 > > by appt: replace max=max[_n-1] if reduct_per<=max[_n-1] & max==. & > time_index==3 > by appt: replace max=reduct_per if max==. & time_index==3 > > by appt: replace max=max[_n-1] if reduct_per<=max[_n-1] & max==. & > time_index==4 > by appt: replace max=reduct_per if max==. & time_index==4 > > by appt: replace max=max[_n-1] if reduct_per<=max[_n-1] & max==. & > time_index==5 > by appt: replace max=reduct_per if max==. & time_index==5 > > by appt: replace max=max[_n-1] if reduct_per<=max[_n-1] & max==. & > time_index==6 > by appt: replace max=reduct_per if max==. & time_index==6 > > and so on. My question is whether there is a more elegant way to > generate this variable. More specifically I would like to know > whether I need to write a loop, and if so how would I write it so that > I don't have to repeat the last two commands for 114 periods > > -- > Dr. Yuval Arbel > School of Business > Carmel Academic Center > 4 Shaar Palmer Street, > Haifa 33031, Israel > e-mail1: yuval.arbel@carmel.ac.il > e-mail2: yuval.arbel@gmail.com > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: How to Calculate A Variable of Maximum Values up to each of the 114 periods in my sample?***From:*Yuval Arbel <yuval.arbel@gmail.com>

- Prev by Date:
**st: Modeling a question with unusual panel data** - Next by Date:
**st: Creating Dummy variables with names from underlying variables** - Previous by thread:
**st: How to Calculate A Variable of Maximum Values up to each of the 114 periods in my sample?** - Next by thread:
**Re: st: How to Calculate A Variable of Maximum Values up to each of the 114 periods in my sample?** - Index(es):