Dear All, I was asked the following question. The data set is
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id int year str1 type
1 1979 "A"
1 1985 "B"
1 1985 "B"
1 1987 "C"
1 1987 "C"
1 1987 "D"
1 1987 "D"
1 1987 "D"
1 1987 "D"
1 1987 "D"
1 1987 "D"
1 1987 "E"
1 1987 "F"
1 1987 "F"
1 1987 "F"
1 1988 "D"
1 1988 "D"
1 1988 "D"
1 1988 "A"
1 1988 "A"
1 1988 "C"
1 1988 "F"
1 1988 "F"
2 1978 "B"
2 1978 "B"
2 1982 "A"
2 1983 "D"
2 1983 "D"
2 1985 "D"
2 1985 "D"
2 1985 "F"
end
The desired outcome is as follows:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id int year str1 type byte number
1 1979 "A" 1
1 1980 "A" 1
1 1981 "A" 1
1 1982 "A" 1
1 1983 "A" 1
1 1984 "A" 1
1 1985 "A" 1
1 1985 "B" 2
1 1986 "A" 1
1 1986 "B" 2
1 1987 "A" 1
1 1987 "B" 2
1 1987 "C" 2
1 1987 "D" 6
1 1987 "E" 1
1 1987 "F" 3
1 1988 "A" 3
1 1988 "B" 2
1 1988 "C" 3
1 1988 "D" 9
1 1988 "E" 1
1 1988 "F" 5
2 1978 "B" 2
2 1979 "B" 2
2 1980 "B" 2
2 1981 "B" 2
2 1982 "A" 1
2 1982 "B" 2
2 1983 "A" 1
2 1983 "B" 2
2 1983 "D" 2
2 1984 "A" 1
2 1984 "B" 2
2 1984 "D" 2
2 1985 "A" 1
2 1985 "B" 2
2 1985 "D" 4
2 1985 "F" 1
For each `id': we'd like to calculate the cumulative counts of different types (A, B, C, ...) over years.
  1. Taking id=1, and type=A for example, it first appeared in 1979. Thus, the number is 1 for that type thereafter. In 1998, there are another 2 As, so the cumulative is 3 from that year.
  2. Taking another instance, id=1, and type=B. It first appeared in 1985 with 2 B, so that the number is 2 from that year on.
  3. As a final example, consider id=1, and type=D in 1987. The number is 6 and the cumulative number is 9 in 1988.
Any suggestions are highly appreciated.