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:

**sysuse ***auto*
**distinct ***rep78*
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.

*Related*

Filed under: Basic functions Tagged: | distinct

Nick Cox, on 28 October 2010 at 9:41 PM said:Thanks for the plug, but anyone interested should install the later and fuller version from the Stata Journal website (not the version from SSC).

The only possible reason to install from SSC is that the version there requires Stata 7 rather than Stata 8.