*! version 1.2.0 November 10, 2017 @ 10:21:51
help unique

Title

unique -- Unique values of a variable or group of variables

Syntax

unique varlist
[if] [in]
[,
by(varlist)
generate(newvar)
detail]

Description

unique reports the number of unique values for the varlist. With a by it does the same, but also creates a new variable (_Unique by default but can be named in the generate option). This new variable contains the number of unique values of the varlist for each level of the by variable. The new variable is coded missing except for the first record in each group defined by the levels of the by variable.

The command is useful for exploring data where the records refer to
more than one level, for example longitudinal data where each record
refers to a visit by a subject.

Options

by(varlist) counts unique values within levels of varlist and stores them in a new variable named _Unique.

generate(varname) supplies an alternative name for the new variable created by by.

detail request summary statistics on the number of records which are
present for unique values of the varlist. If you have longitudinal data
for instance, this option reports the mean, median, minimum and maximum number of visits per subject.

Examples

Consider a longitudinal data set in which each record corresponds to a
visit by a subject. The subject identity is in the variable id, and
the visit is identified by the variable visit, within id. Then

. unique id

reports the number of subjects

. unique id visit

reports a number which will be the same as the number of records unless
there are duplicate records with the same id and visit number.

. unique visit, by(id) gen(num_visits)

creates a new variable num_visits at the subject level which contains the number of visits for that subject. Alternatively

. unique id, detail

reports summary statistics for the number of visits per subject.

Authors

Michael Hills, retired

Tony Brady, Sealed Envelope Ltd, UK (tony@sealedenvelope.com)

Also see

On-line: help for count, egen (tag)