I have a question of how to use stata to calculate the difference of the value of one observation with the average value for observations in other groups
My data structure looks like this:
Obs | Group | Order Value | Difference |
1 | A | 200 | -60 |
2 | A | 120 | -140 |
3 | A | 254 | -6 |
4 | B | 135 | -111.5 |
5 | C | 240 | 62.75 |
6 | C | 265 | 87.75 |
7 | C | 400 | 222.75 |
For instance, for observation 1, I want to calculate the average order value for observations that are in other groups, and then calculate the difference between the order value for observation 1 and the average order value in other groups. (135+240+265+400)/4=260, then I get 200-260=-60; for observation 2, I get 120-260=-140 etc.
I have a huge dataset and can't do it manually. I think I need to write some loops to do that but haven't been able to do that since I'm pretty new to Stata.
I wonder if you know any resources that I can refer to.
Thank you so much!
0 Response to Calculate the difference of the value of one observation with the average value for observations in other groups
Post a Comment