Dear Statalist,

I have a question about constructing count variable. Please refer to my sample data below.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str1 id int year str1 class byte cumulative_count
"a" 2015 "A" 3
"a" 2015 "A" 3
"a" 2015 "B" 3
"a" 2015 "C" 3
"a" 2016 "A" 3
"a" 2016 "A" 3
"a" 2016 "B" 3
"a" 2017 "B" 4
"a" 2017 "D" 4
"a" 2018 "C" 5
"a" 2018 "E" 5
"b" 2015 "A" 1
"b" 2016 "B" 2
"b" 2017 "A" 2
"b" 2017 "B" 2
"b" 2018 "C" 3
end
I want to have a variable 'cumulative_count', which cumulatively counts distinct value of alphabets in 'class' grouped by id.

For example, id "a" has three distinct alphabets "A, B, C" until 2015. So the value of "cumulative_count" in 2015 should be 3.
Until 2018, id "a" has five different alphabets "A, B, C, D, E". So the value of "cumulative_count" in 2018 should be 5.

Thanks a lot for any advice in advance.