夏目贵志 发表于 2015-8-15 06:58 
local obs=10
bys x: egen countx=count(x)
多谢啦  我没讲清楚  第二个问题和第三个问题
如果我有若干个变量a1-a8,我能不能用local x a1-a8,以后用x就可以代替这8个变量?如果可以的话,进一步怎么用
bys x: egen countx=count(x)是不是就是有countx=8? 是bys么?似乎不太对……
第三个问题:set obs=10,我是不是用local m=obs,以后就能用m来代替10这个数字?
我还有个问题是求加权平均,x变量和三个权重变量
clear
input x w1 w2 w3
       4 1 0.2 0.3
          9 0.2 1 0.5
          6 0.3 0.5 1
end
我想分别生成两个变量sumx和sumweight,代表三次加权平均和对应的权重。也就是sumx的第 i 个观测时sum(x*wi);sumweight的第 i 个观测时sum(wi).
 用下面的代码不管用啊~~~~~~~~~
forvalues i=1/3{
    gen sumx=.
        replace sumx=sum(x*weight`i') in `i'
        gen sumweight=.
        replace sumweight=sum(weight`i') in `i'
}
可为什么跑不动……