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