我正在使用具有以下格式的面板数据集
Firm Year Industry Sales Profit Export intensity R&D
1 2000 1 x x x x
2 2000 1 x x x x
3 2000 2 x x x x
4 2000 2 x x x x
1 2001 1 x x x x
2 2001 1 x x x x
3 2001 2 x x x x
4 2001 2 x x x x
1 2002 1 x x x x
2 2002 1 x x x x
3 2002 2 x x x x
4 2002 2 x x x x
1 2003 1 x x x x
2 2003 1 x x x x
3 2003 2 x x x x
4 2003 2 x x x x
我想每年创建所有变量的行业平均值。实际数据集有2000家公司* 10年观测和25个行业。
答案 0 :(得分:1)
如果您想维护数据结构,最简单的方法可能是将egen
的{{1}}选项与循环结合起来:
by()
如,
foreach v of varlist Sales Profit Export RD {
egen IndAvg`v' = mean(`v') , by(Industry Year)
}
但是,您可能还想查看clear all
input Firm Year Industry Sales Profit Export RD
1 2000 1 831 135 196 30
2 2000 1 44 847 885 780
3 2000 2 818 112 859 306
4 2000 2 777 700 903 858
1 2001 1 491 563 325 324
2 2001 1 411 468 927 720
3 2001 2 731 872 170 556
4 2001 2 587 273 833 656
1 2002 1 155 558 497 427
2 2002 1 210 853 792 575
3 2002 2 279 282 969 549
4 2002 2 683 176 902 538
1 2003 1 805 475 479 599
2 2003 1 226 178 37 225
3 2003 2 129 693 746 652
4 2003 2 347 509 406 102
end
foreach v of varlist Sales Profit Export RD {
egen IndAvg`v' = mean(`v') , by(Industry Year)
}
sort Industry Year Firm
li , sepby(Industry)
:
collapse