*! version 1.0.2 23Jan2019

Title

gunique -- Efficiently calculate unique values of a variable or group of variables.

Important: Please run gtools, upgrade to update gtools to
the latest stable version.

Syntax

This is a fast option to the user written unique.
It is 4 to 26 times faster in Stata/IC and 4-12 times faster in MP

gunique
varlist
[if] [in]
[,
detail]

Description

gunique is a faster alternative to unique. It reports the number
of unique values for the varlist. At the moment, its main difference from
distinct is that it always considers the variables jointly. It also has
slighly different options. For example, this supports the by(varlist)
option that also appears in the unique command, but does not support
computing the number of unique values for variables individually.

gunique is part of the [R] gtools project.

Options

by(varlist) counts unique values within levels of varlist and
stores them in a new variable named _Unique. The user can specify the
name of the new variable via the option generate(varname).

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

replace replaces _Unique or the variable specified via generate, if it exists.

detail request summary statistics on the number of records which are
present for unique values of the varlist. With by(), it also prints
the levels with the most unique values.

Gtools

compress Try to compress strL to str#. The Stata Plugin Interface
has only limited support for strL variables. In Stata 13 and earlier
(version 2.0) there is no support, and in Stata 14 and later (version
3.0) there is read-only support. The user can try to compress strL
variables using this option.

forcestrl Skip binary variable check and force gtools to read strL
variables (14 and above only). Gtools gives incorrect results when there is binary data in strL variables.
This option was included because on some windows systems Stata detects
binary data even when there is none. Only use this option if you are
sure you do not have binary data in your strL variables.

verbose prints some useful debugging info to the console.

benchmark and benchmarklevel(int) print how long in
seconds various parts of the program take to execute. The user can also
pass bench(int) for finer control. bench(1) is the same
as benchmark but bench(2) and bench(3) additionally print
benchmarks for internal plugin steps.

hashmethod(str) Hash method to use. default automagically
chooses the algorithm. biject tries to biject the inputs into the
natural numbers. spooky hashes the data and then uses the hash.

oncollision(str) How to handle collisions. A collision should never
happen but just in case it does gtools will try to use native commands.
The user can specify it throw an error instead by passing oncollision(error).

Examples

. sysuse auto

. gunique *

. gunique *, miss

. gunique make-headroom

. gunique make-headroom, d

See the
online documentation
for more examples.

Stored results

gunique stores the following in r():

Scalars

r(N) number of non-missing observations

r(J) number of groups

r(unique) number of groups

r(minJ) largest group size

r(maxJ) smallest group size

Author

Mauricio Caceres Bravo

mauricio.caceres.bravo@gmail.com

mcaceresb.github.io

Website

gunique is maintained as part of [R] gtools at github.com/mcaceresb/stata-gtools

Acknowledgment

gunique was written largely to mimic the functionality of the community-contributed command unique,
written by

Michael Hills, retired

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

This project was largely inspired by Sergio Correia's ftools:
https://github.com/sergiocorreia/ftools.

The OSX version of gtools was implemented with invaluable help from @fbelotti;
see https://github.com/mcaceresb/stata-gtools/issues/11.

Also see

help for
gdistinct,
gtools;
unique (if installed),
ftools (if installed)