Hello, I want to calculate the cumulative returns of each company of the last twelve months (excluding the month itself).
So basically given the example in attach for company 1000 it should not return nothing until 31/mar/1987 (month 12). In mar it should return -0.90714 given the calculation [(1+(-0,25714))*(1+0,365385)*(1+(-0,09859))*...*(1+(-0,37736))*(1+(-0,21212))]-1. In April it should return 0,092308 given the calculation [(1+0,365285)*(1+(-0,09859))*...*(1+(-0,38462))]-1. And so on.
It should do this per company.
I used the following code but it misses 1 observation per company:
sort permno date
gen id=_n
sort permno date
. by permno: gen newid = 1 if _n==1
. replace newid = sum(newid)
. replace newid = . if missing(permno)
sort permno date
. by permno: generate sumlogr = sum(ln(1 + ret))
. by permno: gen sumlogr1=sumlogr[_n-1]
xtset newid id
generate r12 = exp(s12.sumlogr1) - 1
I also have the problem that sometimes there are month that do not exist.. If possible in other code could you do the exact something but returning the cumulative returns for the last year instead of last 12 months? So in the example in march it should return -0,365385 given the calculation [(1+0,365385)*(1+(-0,09859))*...*(1+(0,0,21212))]-1. This means for each month it starts in that month in the previous year and ends the closest possible (in this case January 1987).
Thanks and I hope you can help me!
So basically given the example in attach for company 1000 it should not return nothing until 31/mar/1987 (month 12). In mar it should return -0.90714 given the calculation [(1+(-0,25714))*(1+0,365385)*(1+(-0,09859))*...*(1+(-0,37736))*(1+(-0,21212))]-1. In April it should return 0,092308 given the calculation [(1+0,365285)*(1+(-0,09859))*...*(1+(-0,38462))]-1. And so on.
It should do this per company.
I used the following code but it misses 1 observation per company:
sort permno date
gen id=_n
sort permno date
. by permno: gen newid = 1 if _n==1
. replace newid = sum(newid)
. replace newid = . if missing(permno)
sort permno date
. by permno: generate sumlogr = sum(ln(1 + ret))
. by permno: gen sumlogr1=sumlogr[_n-1]
xtset newid id
generate r12 = exp(s12.sumlogr1) - 1
I also have the problem that sometimes there are month that do not exist.. If possible in other code could you do the exact something but returning the cumulative returns for the last year instead of last 12 months? So in the example in march it should return -0,365385 given the calculation [(1+0,365385)*(1+(-0,09859))*...*(1+(0,0,21212))]-1. This means for each month it starts in that month in the previous year and ends the closest possible (in this case January 1987).
Thanks and I hope you can help me!
Comment