*! version 2.11.0 08jun2017

Title

flevelsof -- Levels of variable

Syntax

flevelsof
varname
[if] [in]
[, options]

clean display string values without compound double quotes

local(macname) insert the list of values in the local macro macname

missing include missing values of varname in calculation

separate(separator) separator to serve as punctuation for the values of returned list; default is a space

forcemata prevents calling levelsof for datasets with less than 1mm obs.

verbose display debugging information

Description

flevelsof displays a sorted list of the distinct values of varname.

Options

clean displays string values without compound double quotes.
By default, each distinct string value is displayed within compound double
quotes, as these are the most general delimiters. If you know that the
string values in varname do not include embedded spaces or embedded
quotes, this is an appropriate option. clean
does not affect the display of values from numeric variables.

local(macname) inserts the list of values in
local macro macname within the calling program's space. Hence,
that macro will be accessible after flevelsof has finished.
This is helpful for subsequent use, especially with foreach.

missing specifies that missing values of varname
should be included in the calculation. The default is to exclude them.

separate(separator) specifies a separator
to serve as punctuation for the values of the returned list.
The default is a space. A useful alternative is a comma.

Remarks

flevelsof serves two different functions. First, it gives a
compact display of the distinct values of varname. More commonly, it is
useful when you desire to cycle through the distinct values of
varname with (say) foreach; see [P] foreach.
flevelsof leaves behind a list in r(levels) that may be used in a
subsequent command.

flevelsof may hit the limits imposed by your Stata. However,
it is typically used when the number of distinct values of
varname is modest.

The terminology of levels of a factor has long been standard in
experimental design. See
Cochran and Cox (1957, 148),
Fisher (1942), or
Yates (1937, 5).

Examples

. sysuse auto

. flevelsof rep78

. display "`r(levels)'"

. flevelsof rep78, miss local(mylevs)

. display "`mylevs'"

. flevelsof rep78, sep(,)

. display "`r(levels)'"

Showing value labels when defined:

. flevelsof factor, local(levels)

. foreach l of local levels {

. di "-> factor = `: label (factor) `l''"

. whatever if factor == `l'

. }

Stored results

flevelsof stores the following in r():

Macros

r(levels) list of distinct values

References

Cochran, W. G., and G. M. Cox. 1957. Experimental Designs. 2nd ed.
New York: Wiley.

Fisher, R. A. 1942. The theory of confounding in factorial experiments in
relation to the theory of groups.
Annals of Eugenics 11: 341-353.

Yates, F. 1937. The Design and Analysis of Factorial Experiments.
Harpenden, England: Technical Communication 35, Imperial Bureau of
Soil Science.