我有以下数据:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id int(year month) byte sale float revenue
1 1999 3 0 1632.5
1 1999 2 1 2658.9
1 1999 1 1 2504.6
1 1998 1 1 1993.7
1 1998 3 1 1883.1
1 1998 2 0 1809.5
1 1997 2 1 1707.7
1 1997 3 0 1695.7
1 1997 1 1 1895.3
1 1996 2 0 1649.1
1 1996 3 0 1662.5
1 1996 1 0 1749.5
2 1995 1 0 1665
2 1995 2 0 1624.4
2 1995 3 0 1701.9
2 1994 2 0 1688.3
2 1994 1 1 1714
2 1994 3 0 1716.6
2 1993 2 0 1750.8
2 1993 1 0 1655.7
2 1993 3 0 1746.7
2 1992 1 0 1670.5
2 1992 3 0 1665.2
2 1992 2 1 1558.7
3 1991 1 0 1623.1
3 1991 3 0 1602.1
3 1991 2 0 1617.1
3 1990 1 1 1486.9
3 1990 2 0 1630.7
3 1990 3 0 1653
3 1989 2 0 1755.2
3 1989 3 1 1764.2
3 1989 1 0 1662.2
3 1988 3 0 1842.9
3 1988 2 0 1778.7
3 1988 1 0 1773.2
end
我希望在ID和月份窗口中为每笔交易找到与交易年份相对的位置。
这是预期的输出:
1 1996 1 0 1749.5 -1 -2 -3
1 1997 1 1 1895.3 0 -1 -2
1 1998 1 1 1993.7 1 0 -1
1 1999 1 1 2504.6 2 1 0
1 1996 2 0 1649.1 -1 -3
如何在Stata中做到这一点?
答案 0 :(得分:1)
以下为每个销售/月创建一个变量:
sort id month year
by id month: generate n = _n
by id month: generate sales = sum(sale)
summarize sales
forvalues i = 1 / `r(max)' {
by id month: generate tag1 = n if sale & sales == `i'
by id month: egen tag2 = total(tag1)
generate saleno`i' = n - tag2 if tag2 > 0
drop tag*
}
drop n sales
list, sepby(id month)
+---------------------------------------------------------------------+
| id year month sale revenue saleno1 saleno2 saleno3 |
|---------------------------------------------------------------------|
1. | 1 1996 1 0 1749.5 -1 -2 -3 |
2. | 1 1997 1 1 1895.3 0 -1 -2 |
3. | 1 1998 1 1 1993.7 1 0 -1 |
4. | 1 1999 1 1 2504.6 2 1 0 |
|---------------------------------------------------------------------|
5. | 1 1996 2 0 1649.1 -1 -3 . |
6. | 1 1997 2 1 1707.7 0 -2 . |
7. | 1 1998 2 0 1809.5 1 -1 . |
8. | 1 1999 2 1 2658.9 2 0 . |
|---------------------------------------------------------------------|
9. | 1 1996 3 0 1662.5 -2 . . |
10. | 1 1997 3 0 1695.7 -1 . . |
11. | 1 1998 3 1 1883.1 0 . . |
12. | 1 1999 3 0 1632.5 1 . . |
|---------------------------------------------------------------------|
13. | 2 1992 1 0 1670.5 -2 . . |
14. | 2 1993 1 0 1655.7 -1 . . |
15. | 2 1994 1 1 1714 0 . . |
16. | 2 1995 1 0 1665 1 . . |
|---------------------------------------------------------------------|
17. | 2 1992 2 1 1558.7 0 . . |
18. | 2 1993 2 0 1750.8 1 . . |
19. | 2 1994 2 0 1688.3 2 . . |
20. | 2 1995 2 0 1624.4 3 . . |
|---------------------------------------------------------------------|
21. | 2 1992 3 0 1665.2 . . . |
22. | 2 1993 3 0 1746.7 . . . |
23. | 2 1994 3 0 1716.6 . . . |
24. | 2 1995 3 0 1701.9 . . . |
|---------------------------------------------------------------------|
25. | 3 1988 1 0 1773.2 -2 . . |
26. | 3 1989 1 0 1662.2 -1 . . |
27. | 3 1990 1 1 1486.9 0 . . |
28. | 3 1991 1 0 1623.1 1 . . |
|---------------------------------------------------------------------|
29. | 3 1988 2 0 1778.7 . . . |
30. | 3 1989 2 0 1755.2 . . . |
31. | 3 1990 2 0 1630.7 . . . |
32. | 3 1991 2 0 1617.1 . . . |
|---------------------------------------------------------------------|
33. | 3 1988 3 0 1842.9 -1 . . |
34. | 3 1989 3 1 1764.2 0 . . |
35. | 3 1990 3 0 1653 1 . . |
36. | 3 1991 3 0 1602.1 2 . . |
+---------------------------------------------------------------------+