*! version 18.0 07jul2018

Title

distinct is an egen function to compute the number of distinct
observations of a varlist

Syntax

[by varlist:] egen [type] newvar = distinct(varlist) [if] [in] [bymissok]

Description

distinct is an egen function that generates a new variable
holding a count of the number of distinct observation of a given varlist,
optionally by another varlist.

When if or in is specified, the conditions apply both to counting the
distinct values and to generating the new variable. The new variable will be
missing in observation excluded by the if or in condition.

If the if and in conditions exclude one of the values of
varlist, the value of the new variable will reflect that.

See section Missing Values below for information on how distinct
handles missing values in the varlist and bysort.

Examples

Suppose we have person-level data for individuals in the US states Connecticut
and Massachusetts. The data include which town a person lives in. To
generate a new variable counting the total number of distinct towns in the
data:

egen N_towns = distinct(town)

To seperately count the number of distinct towns by state:

by state: egen N_towns_by_state = distinct(town)

To count the distinct number of towns not named "Sharon" by state (there is
a Sharon in each state):

by state: egen N_not_sharon_by_state = distinct(town) if town != "Sharon"

Note that N_not_sharon_by_state will be missing for observations
where town is "Sharon".

Missing Values

There are two places where missing values are relevant to distinct.

The first is in the list of variables you provide as the bysort variables.
If you specify option bymissok, distinct will treat missing
values in the by variables as valid, and count the number of distinct values
as though the missing values in the by variables were non-missing.

The second is in the list of variables for which distinct observations are
to be counted. distinct skips these values.

Remarks

See distinct for a definition of the adjective "distinct" in this
context.

A test script for egen distinct is available at
https://github.com/noahjcase/egen-distinct

Author

Noah J. Case, District of Columbia, USA.
noahcase119@gmail.com