Hello,
I would like to calculate the mean of my average stock returns grouped by another variable, which splits my oberservations in 3 groups.
Basically i want to replicate this formula:
EMI = efficient minus inefficient
I tried something like:
bysort SOX_size3 re_year Month : gen EMI1_SOX_e = avgSOXreturn100 if SOX_size3==1
and then take this as a new generated variable and tried to substract it from
bysort SOX_size3 re_year Month : gen EMI1_SOX_ie = avgSOXreturn100 if SOX_size3==3
as a new generated variable, to get something like: EMI_SOX
But this is not working because the data looks in a similar way like this, if I sort my data on my criteria (SOX_size3, re_year and Month):
For SOX_size3: 1 means efficient and 3 means inefficient.
EMI_SOX should be the average of EMI_SOX_e minus EMI_SOX_ie.
Missing Values can be ignored.
I am pretty sure to use something with bysort, generate and sort in Stata, but I cannot figure it out. I hope someone of you can help.
Below you find my dataex extract.
Thanks.
I would like to calculate the mean of my average stock returns grouped by another variable, which splits my oberservations in 3 groups.
Basically i want to replicate this formula:
EMI = efficient minus inefficient
I tried something like:
bysort SOX_size3 re_year Month : gen EMI1_SOX_e = avgSOXreturn100 if SOX_size3==1
and then take this as a new generated variable and tried to substract it from
bysort SOX_size3 re_year Month : gen EMI1_SOX_ie = avgSOXreturn100 if SOX_size3==3
as a new generated variable, to get something like: EMI_SOX
But this is not working because the data looks in a similar way like this, if I sort my data on my criteria (SOX_size3, re_year and Month):
SOX_size3 | avgSOXreturn | EMI_SOX_e | EMISOX_ie | EMI_SOX |
1 | 123 | 123 | missing values | 123 |
1 | 123 | 123 | missing values | 123 |
1 | 123 | 123 | missing values | 123 |
1 | 123 | 123 | missing values | 123 |
1 | 123 | 123 | missing values | 123 |
2 | 123 | missing values | missing values | 123 |
2 | 123 | missing values | missing values | 123 |
2 | 123 | missing values | missing values | 123 |
2 | 123 | missing values | missing values | 345 |
2 | 123 | missing values | missing values | 345 |
3 | 123 | missing values | 345 | 345 |
3 | 123 | missing values | 345 | 345 |
3 | 123 | missing values | 345 | 345 |
3 | 123 | missing values | 345 | 345 |
3 | 123 | missing values | 345 | 345 |
3 | 123 | missing values | 345 | 345 |
EMI_SOX should be the average of EMI_SOX_e minus EMI_SOX_ie.
Missing Values can be ignored.
I am pretty sure to use something with bysort, generate and sort in Stata, but I cannot figure it out. I hope someone of you can help.
Below you find my dataex extract.
Thanks.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long RIC_n int re_year byte Month double SOX_size3 float(avgSOXreturn100 EMI1_SOX_e EMI1_SOX_ie) 3 2009 1 1 .09465913 .09465913 . 4 2009 1 1 .09465913 .09465913 . 6 2009 1 1 .09465913 .09465913 . 7 2009 1 1 .09465913 .09465913 . 9 2009 1 1 .09465913 .09465913 . 11 2009 1 1 .09465913 .09465913 . 14 2009 1 1 .09465913 .09465913 . 23 2009 1 1 .09465913 .09465913 . 26 2009 1 1 .09465913 .09465913 . 27 2009 1 1 .09465913 .09465913 . 34 2009 1 1 .09465913 .09465913 . 42 2009 1 1 .09465913 .09465913 . 46 2009 1 1 .09465913 .09465913 . 48 2009 1 1 .09465913 .09465913 . 50 2009 1 1 .09465913 .09465913 . 59 2009 1 1 .09465913 .09465913 . 60 2009 1 1 .09465913 .09465913 . 3 2009 2 1 -.4017774 -.4017774 . 4 2009 2 1 -.4017774 -.4017774 . 6 2009 2 1 -.4017774 -.4017774 . 7 2009 2 1 -.4017774 -.4017774 . 9 2009 2 1 -.4017774 -.4017774 . 11 2009 2 1 -.4017774 -.4017774 . 14 2009 2 1 -.4017774 -.4017774 . 23 2009 2 1 -.4017774 -.4017774 . 26 2009 2 1 -.4017774 -.4017774 . 27 2009 2 1 -.4017774 -.4017774 . 34 2009 2 1 -.4017774 -.4017774 . 42 2009 2 1 -.4017774 -.4017774 . 46 2009 2 1 -.4017774 -.4017774 . 48 2009 2 1 -.4017774 -.4017774 . 50 2009 2 1 -.4017774 -.4017774 . 59 2009 2 1 -.4017774 -.4017774 . 60 2009 2 1 -.4017774 -.4017774 . 3 2009 3 1 .24362627 .24362627 . 4 2009 3 1 .24362627 .24362627 . 6 2009 3 1 .24362627 .24362627 . 7 2009 3 1 .24362627 .24362627 . 9 2009 3 1 .24362627 .24362627 . 11 2009 3 1 .24362627 .24362627 . 14 2009 3 1 .24362627 .24362627 . 23 2009 3 1 .24362627 .24362627 . 26 2009 3 1 .24362627 .24362627 . 27 2009 3 1 .24362627 .24362627 . 34 2009 3 1 .24362627 .24362627 . 42 2009 3 1 .24362627 .24362627 . 46 2009 3 1 .24362627 .24362627 . 48 2009 3 1 .24362627 .24362627 . 50 2009 3 1 .24362627 .24362627 . 59 2009 3 1 .24362627 .24362627 . 60 2009 3 1 .24362627 .24362627 . 3 2009 4 1 .8238223 .8238223 . 4 2009 4 1 .8238223 .8238223 . 6 2009 4 1 .8238223 .8238223 . 7 2009 4 1 .8238223 .8238223 . 9 2009 4 1 .8238223 .8238223 . 11 2009 4 1 .8238223 .8238223 . 14 2009 4 1 .8238223 .8238223 . 23 2009 4 1 .8238223 .8238223 . 26 2009 4 1 .8238223 .8238223 . 27 2009 4 1 .8238223 .8238223 . 34 2009 4 1 .8238223 .8238223 . 42 2009 4 1 .8238223 .8238223 . 46 2009 4 1 .8238223 .8238223 . 48 2009 4 1 .8238223 .8238223 . 50 2009 4 1 .8238223 .8238223 . 59 2009 4 1 .8238223 .8238223 . 60 2009 4 1 .8238223 .8238223 . 3 2009 5 1 .59562814 .59562814 . 4 2009 5 1 .59562814 .59562814 . 6 2009 5 1 .59562814 .59562814 . 7 2009 5 1 .59562814 .59562814 . 9 2009 5 1 .59562814 .59562814 . 11 2009 5 1 .59562814 .59562814 . 14 2009 5 1 .59562814 .59562814 . 23 2009 5 1 .59562814 .59562814 . 26 2009 5 1 .59562814 .59562814 . 27 2009 5 1 .59562814 .59562814 . 34 2009 5 1 .59562814 .59562814 . 42 2009 5 1 .59562814 .59562814 . 46 2009 5 1 .59562814 .59562814 . 48 2009 5 1 .59562814 .59562814 . 50 2009 5 1 .59562814 .59562814 . 59 2009 5 1 .59562814 .59562814 . 60 2009 5 1 .59562814 .59562814 . 3 2009 6 1 -.2812882 -.2812882 . 4 2009 6 1 -.2812882 -.2812882 . 6 2009 6 1 -.2812882 -.2812882 . 7 2009 6 1 -.2812882 -.2812882 . 9 2009 6 1 -.2812882 -.2812882 . 11 2009 6 1 -.2812882 -.2812882 . 14 2009 6 1 -.2812882 -.2812882 . 23 2009 6 1 -.2812882 -.2812882 . 26 2009 6 1 -.2812882 -.2812882 . 27 2009 6 1 -.2812882 -.2812882 . 34 2009 6 1 -.2812882 -.2812882 . 42 2009 6 1 -.2812882 -.2812882 . 46 2009 6 1 -.2812882 -.2812882 . 48 2009 6 1 -.2812882 -.2812882 . 50 2009 6 1 -.2812882 -.2812882 . end label values RIC_n RIC_n label def RIC_n 3 "AKERBP.OL", modify label def RIC_n 4 "BASFn.F", modify label def RIC_n 6 "BWO.OL", modify label def RIC_n 7 "CLB", modify label def RIC_n 9 "ENAG.MC", modify label def RIC_n 11 "EQNR.OL", modify label def RIC_n 14 "EUAV.BR", modify label def RIC_n 23 "HBR.L", modify label def RIC_n 26 "LTSP.WA", modify label def RIC_n 27 "LUNE.ST", modify label def RIC_n 34 "OMVV.VI", modify label def RIC_n 42 "REC.OL", modify label def RIC_n 46 "ROSNP.BX", modify label def RIC_n 48 "SBMO.AS", modify label def RIC_n 50 "SGREN.MC", modify label def RIC_n 59 "VLLP.PA", modify label def RIC_n 60 "VOPA.AS", modify label values SOX_size3 SOX_size3l label def SOX_size3l 1 "1 (Efficient within CO2)", modify
Comment