I previously asked this question on the Stata forum, but now wish to implement it in Mata.
I have data that looks like the following:
In the above, Individual corresponds to the individual ID and vacation destination corresponds to the destination that the individual visited in a particular year. I wish to create a variable that counts the number of unique destinations an individual went to. This variable, call it C, will take on a value of 2 for individual 1, 3 for individual 2 and so on
As of now, what I have done on mata is as follows.
Although I have obtained the values of unique datapoints, how can I count the number of unique datapoints?
I have data that looks like the following:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(Individual VacationDestination) 1 12 1 12 1 14 1 14 2 12 2 15 2 16 3 12 3 12 3 12 4 1 4 2 4 3 end
In the above, Individual corresponds to the individual ID and vacation destination corresponds to the destination that the individual visited in a particular year. I wish to create a variable that counts the number of unique destinations an individual went to. This variable, call it C, will take on a value of 2 for individual 1, 3 for individual 2 and so on
As of now, what I have done on mata is as follows.
Code:
C=(sort(Individual,1),VacationDestination) z=uniqrows(C)
Comment