Hi 
I have a question about generating a variable, which is a variance of another variable.
Here is the context:
- I have panel data of 2 firms.
- Minimum data period is monthly.
- I have a variable "return," a monthly abnormal return.
- What I want to get is the variance (fluctuation) of the 'return' for each firm and for each 'timeid'.
The code I tried:
xtset id timeid
egen variance = sd(return), by(id)
replace variance = variance^2
-> this gave me values that were the same for each id.
-> But, as mentioned above, I want to get the fluctuation of the monthly return by company.
So, I tried
egen variance = sd(return), by(timeid)
replace variance = variance^2
Then, I got the different variances for each timeid, but I guess this value did not consider the "id."
Can you help me how to resolve this issue?
Thank you very much in advance.
Below is the simple version of my dataset.

I have a question about generating a variable, which is a variance of another variable.
Here is the context:
- I have panel data of 2 firms.
- Minimum data period is monthly.
- I have a variable "return," a monthly abnormal return.
- What I want to get is the variance (fluctuation) of the 'return' for each firm and for each 'timeid'.
The code I tried:
xtset id timeid
egen variance = sd(return), by(id)
replace variance = variance^2
-> this gave me values that were the same for each id.
-> But, as mentioned above, I want to get the fluctuation of the monthly return by company.
So, I tried
egen variance = sd(return), by(timeid)
replace variance = variance^2
Then, I got the different variances for each timeid, but I guess this value did not consider the "id."
Can you help me how to resolve this issue?
Thank you very much in advance.
Below is the simple version of my dataset.
id | year | month | timeid | return |
1 | 2010 | 1 | 1 | 3.46 |
1 | 2010 | 2 | 2 | 5.22 |
1 | 2010 | 3 | 3 | 2.71 |
1 | 2010 | 4 | 4 | 4.90 |
1 | 2010 | 5 | 5 | 4.33 |
1 | 2010 | 6 | 6 | 5.13 |
1 | 2010 | 7 | 7 | 4.03 |
1 | 2010 | 8 | 8 | 3.63 |
1 | 2010 | 9 | 9 | 2.98 |
1 | 2010 | 10 | 10 | 3.96 |
1 | 2010 | 11 | 11 | 4.90 |
1 | 2010 | 12 | 12 | 2.56 |
1 | 2011 | 1 | 13 | 5.97 |
1 | 2011 | 2 | 14 | 4.82 |
1 | 2011 | 3 | 15 | 3.14 |
1 | 2011 | 4 | 16 | 2.51 |
1 | 2011 | 5 | 17 | 5.92 |
1 | 2011 | 6 | 18 | 5.28 |
1 | 2011 | 7 | 19 | 5.71 |
1 | 2011 | 8 | 20 | 2.59 |
1 | 2011 | 9 | 21 | 2.15 |
1 | 2011 | 10 | 22 | 5.72 |
1 | 2011 | 11 | 23 | 4.54 |
1 | 2011 | 12 | 24 | 5.51 |
2 | 2010 | 1 | 1 | 4.24 |
2 | 2010 | 2 | 2 | 2.53 |
2 | 2010 | 3 | 3 | 3.49 |
2 | 2010 | 4 | 4 | 5.72 |
2 | 2010 | 5 | 5 | 4.54 |
2 | 2010 | 6 | 6 | 4.19 |
2 | 2010 | 7 | 7 | 4.89 |
2 | 2010 | 8 | 8 | 3.96 |
2 | 2010 | 9 | 9 | 5.31 |
2 | 2010 | 10 | 10 | 3.04 |
2 | 2010 | 11 | 11 | 2.40 |
2 | 2010 | 12 | 12 | 2.68 |
2 | 2011 | 1 | 13 | 3.24 |
2 | 2011 | 2 | 14 | 4.33 |
2 | 2011 | 3 | 15 | 3.76 |
2 | 2011 | 4 | 16 | 3.12 |
2 | 2011 | 5 | 17 | 4.83 |
2 | 2011 | 6 | 18 | 3.47 |
2 | 2011 | 7 | 19 | 5.90 |
2 | 2011 | 8 | 20 | 5.45 |
2 | 2011 | 9 | 21 | 2.72 |
2 | 2011 | 10 | 22 | 2.77 |
2 | 2011 | 11 | 23 | 5.01 |
2 | 2011 | 12 | 24 | 3.34 |
Comment