黃河泉 发表于 2017-7-28 08:37 
请试试
可以用简单的方法:先把volume为0的年份也补上,然后再
xtset id year
gen v_acc = .
replace v_acc = volume if year == 2008
replace v_acc = volume + volume[_n-1] if year == 2009
replace v_acc = volume + volume[_n-1] + volume[_n-2] if year == 2010
replace v_acc = volume + volume[_n-1] + volume[_n-2] + volume[_n-3] if year == 2011
但是如果年份比较多,一句一句写太麻烦,怎样用循环语句做到呢?