问题描述
我正在尝试编写一个循环,但这是不正确的。
levelsof id2,local(levels)
foreach l of local levels {
gen totalgovtake = sum(Real_Gov_Take_MUSD)
}
解决方法
您实际上不需要在这里使用循环。我创建了一些模拟数据来说明解决方案(请在下次您提问题时尝试提供模拟数据)。
input id value
1 20
1 30
1 25
2 60
2 50
3 10
3 20
4 50
end
bysort id: egen total = sum(value)
list,sepby(id)
+--------------------+
| id value total |
|--------------------|
1. | 1 20 75 |
2. | 1 30 75 |
3. | 1 25 75 |
|--------------------|
4. | 2 60 110 |
5. | 2 50 110 |
|--------------------|
6. | 3 10 30 |
7. | 3 20 30 |
|--------------------|
8. | 4 50 50 |
+--------------------+