Posted on 28 October 2010 by Mitch Abdon
In Ways to count the number of unique values in a variable, we have suggested different ways on how to get the number of unique values for a variable. Forget what we have suggested. Instead, use -distinct- (Longton and Cox) to display the number of unique values in a variable or a set of variables (see note). The command is very straight forward. For example:
will display the total number of non-missing observations and the total number of unique values for rep78. -distinct- has other options and may be used with the prefix -by-. For more, type “help distinct”. But first, install the command by typing:
ssc install distinct
Details are available in Speaking Stata: Distinct observations (Cox and Longton 2008).
Note: Thanks to Nick Cox for pointing this out in his comment.