Dear Statalist,
I am trying to generate the maximum of the variable ret on a two year rolling window. So for year 1979, I would like to have 0.1135 (as this is the max of ret of year 1977&1978), for year 1980 I would like it to be 0.1135(as this is max of ret of year 1978&1979) and for year 1981 if would like it to be 0.1092(as this is the max of 1979&1980) ect..
In the process of achieving this I started with forvalues, but this does not seem to work with year==`i'[_n+1], I get the error '1977 invalid name', so after some research I have now discovered tsegen and rangestat.
I have tried the following codes that produced ret_max, max2 and max3:
I understand that my code with forvalues is not correct at all, and I don't understand how I could make it work for multiple years when it doesn't work with [_n+1]. For forvalues I tried with this code:
I hope someone out there can help me, thank you all Statalist experts!
My data:
I am trying to generate the maximum of the variable ret on a two year rolling window. So for year 1979, I would like to have 0.1135 (as this is the max of ret of year 1977&1978), for year 1980 I would like it to be 0.1135(as this is max of ret of year 1978&1979) and for year 1981 if would like it to be 0.1092(as this is the max of 1979&1980) ect..
In the process of achieving this I started with forvalues, but this does not seem to work with year==`i'[_n+1], I get the error '1977 invalid name', so after some research I have now discovered tsegen and rangestat.
I have tried the following codes that produced ret_max, max2 and max3:
Code:
rangestat (max) ret, interval(year 0 2)
Code:
tsegen max2 = rowmax(L(0/2).ret, 2)
Code:
tsegen max3 = rowmax(L(1/3).ret, 2)
Code:
gen maxret = . gen temp = 0 forvalues i=1977/2016 { replace temp = 0 replace temp = 1 if year==`i' replace temp = 1 if year==`i'[_n+1] egen max = max(ret) if temp==1 replace maxret = max if year ==`i' drop max }
My data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double year float ret double ret_max float(max2 max3) 1975 . . . . 1976 . . . . 1977 -.0082 .11349999904632568 . . 1978 .1135 .11349999904632568 .1135 . 1979 .1092 .33730000257492065 .1135 .1135 1980 .0624 .33730000257492065 .1135 .1135 1981 .3373 1.242900013923645 .3373 .1135 1982 .1269 1.242900013923645 .3373 .3373 1983 1.2429 1.242900013923645 1.2429 .3373 1984 .4863 .49630001187324524 1.2429 1.2429 1985 .4963 .49630001187324524 1.2429 1.2429 1986 .10867027 .3816371262073517 .4963 1.2429 1987 .011902425 1.0858999490737915 .4963 .4963 1988 .3816371 1.0858999490737915 .3816371 .4963 1989 1.0859 1.0858999490737915 1.0859 .3816371 1990 .0029 .019600000232458115 1.0859 1.0859 1991 -.011679173 1.0448999404907227 1.0859 1.0859 1992 .0196 1.0448999404907227 .0196 1.0859 1993 1.0449 1.0448999404907227 1.0449 .0196 1994 .1093 .48660001158714294 1.0449 1.0449 1995 .1048 .48660001158714294 1.0449 1.0449 1996 .4866 .48660001158714294 .4866 1.0449 1997 .4115 .7723000049591065 .4866 .4866 1998 -.02070992 .7723000049591065 .4866 .4866 1999 .7723 .7723000049591065 .7723 .4866 2000 .3607 .36070001125335693 .7723 .7723 2001 -.0026761396 .6405687928199768 .7723 .7723 2002 -.002508564 .6405687928199768 .3607 .7723 2003 .6405688 .6405687928199768 .6405688 .3607 2004 .3816 .5199000239372253 .6405688 .6405688 2005 .5199 .5199000239372253 .6405688 .6405688 2006 .2403 .2402999997138977 .5199 .6405688 2007 .1723 .8766999840736389 .5199 .5199 2008 -.020267397 .8766999840736389 .2403 .5199 2009 .8767 .8766999840736389 .8767 .2403 2010 .2325 .24420000612735748 .8767 .8767 2011 .0616 .2897999882698059 .8767 .8767 2012 .2442 .2897999882698059 .2442 .8767 2013 .2898 .2897999882698059 .2898 .2442 2014 .0036222886 .3154999911785126 .2898 .2898 2015 .007412713 .3154999911785126 .2898 .2898 2016 .3155 .3154999911785126 .3155 .2898 end
Comment