help for groups

List group frequencies and percents

groups
varlist
[weight]
[if exp]
[in range]
[,

specification_options

presentation_options

saving_option

]

by ...: may be used with groups; see help
by. Note in particular that this is one key to controlling how
percents are calculated; that is, under by: percents sum to 100
within distinct categories defined by its varlist.

fweights and aweights are allowed; see help weights.

Description

groups lists the distinct groups of varlist occurring in the
dataset and their frequencies and/or percents.
groups is perhaps most useful with
categorical variables, but has other uses. Groups are by default
presented in the sort order of varlist. Note that there is no limit
on the number of variables in varlist.

Frequencies are counts or other measures of abundance.

Percents are percents of each total frequency.

Cumulative frequencies and percents are cumulated in the order of groups
and show frequency (percent) in each group and all earlier groups in the
listing, unless the lt option is specified.

Reverse cumulative frequencies and percents show frequency (percent) in
all later groups in the listing, unless the ge option is
specified.

"Valid" percents are calculated relative to all pertinent
non-missing values.

Options

specification

fillin show cross-combinations with zero values

ge reverse frequencies,
percents for current and later groups

lt cumulatives for previous groups

missing show missing values

select({condition|#}) selecting results

show(what_to_show) specify frequencies, percents, etc.

percent(varlist) variables defining which percents calculated

fillin specifies that groups (i.e. cross-combinations) of
varlist which do not occur in the data are shown explicitly as
having zero frequency. This has no effect with a single variable. Note
that this option can bite hard as the number of cross-combinations can
explode combinatorially.

ge (think greater than or equal to)
specifies that reverse frequencies and percents are to be
calculated for the current and all later groups, that is, they are for
values greater than or equal to each value.

lt (think less than)
specifies that cumulative frequencies and percents are to be
calculated for only the previous groups, that is, they are for values
less than each value.

missing specifies that observations with missing values on any of
the variables in varlist are to be included in the listing. By
default they are omitted. Note that "valid" percents will be the same
as other percents unless the missing option is specified.

select() specifies that only selected groups are to be listed.
There are two syntaxes.

In the first syntax, selection is according to a condition imposed on
the frequencies, or on the percents, or on the cumulative frequencies,
or on the cumulative percents, or on the reverse cumulatives. The syntax
is exemplified by

select(freq == 1)

select(percent > 5)

select(Percent < 50)

The elements freq, percent, Freq, Percent,
RFreq, RPercent, vpercent, Vpercent or
rvpercent may be abbreviated down to unambiguous abbreviations.
Note that case matters in distinguishing freq and Freq,
percent and Percent, and vpercent and Vpercent.
What follows must complete a simple true-or-false condition in Stata
syntax, typically an inequality or equality.

In the second syntax, a positive or negative integer is specified. A
positive integer specifies that only the first # groups are to be
shown. A negative integer specifies that only the last
|#| groups are to be shown.

First and last are determined with
respect to the listing which would otherwise have been given.

Thus with order(h), select(5) shows the 5 groups with the 5
highest frequencies, while select(-5) shows the 5 groups with the
5 lowest frequencies, ties being broken according to the sort order of
varlist.

Similarly, with order(l) the opposite is true. Without
order(), select(5) shows the first 5 groups of varlist
and select(-5) shows the last 5 groups of varlist. The most
obviously useful example is when varlist consists of a single
variable, so that the listing is of the 5 lowest (highest) groups of
values of that variable.

show() specifies which frequencies should be shown. By default,
frequencies, percents and cumulative percents are shown with one
variable, and frequencies and percents are shown with two or more
variables, in that order. show() may be used to specify one or
two or three of those, and/or cumulative frequencies, and/or reverse
cumulative frequencies or reverse cumulative percents, and/or equivalent
percents for "valid" values, or to change the order of presentation.
The elements freq, percent, Freq, Percent,
RFreq, vpercent, Vpercent and
rvpercent may be abbreviated, down to unambiguous abbreviations.
Note that case matters in distinguishing freq and Freq,
percent and Percent, or
vpercent and Vpercent.

Exceptionally, show(none) may be used to specify that none of these
should be shown. For example, with select(f == 1) the frequencies
would all be 1, and thus their display may seem unnecessary, while the
percents and cumulative percents may not be of interest,
so show(none) may be desired.

percent() specifies that percents and cumulatives are calculated
with respect to the combinations of the variables specified. The results
shown will resemble those with by:, except that the variables
named are displayed within each body of results. The default is that
percents and cumulatives are calculated with respect to all observations
selected.

For example, the same numerical results will appear for

. bysort foreign: groups rep78

and for

. groups foreign rep78, percent(foreign)

so that in either case percents are calculated for groups defined
by distinct categories of foreign.

presentation

format(format) display format for percents

list_options options of list

order({high|low}) order by frequency

reverse reverse display from default

showhead(text) header text for frequencies

colorder(integers) reorder or repeat columns of table

format() specifies a numeric format for percent and cumulative
percent frequencies. The default is %6.2f.

list_options are options of list. These offer several ways
of changing the appearance of the listing. Note that sum by itself
produces sums only of frequencies and percents, where shown.
sepby() and sep() are often especially helpful.

order() specifies that groups should be listed in order of their
frequencies. Ordering may be high (highest frequencies first) or
low (lowest frequencies first). high and low may be
abbreviated, down to as little as h or l respectively.

reverse reverses what would otherwise be displayed, last values first.

showhead() specifies alternative text for the header explaining
frequency variables. There should be as many elements as the number of
frequency, percent, cumulative frequency, cumulative percent, reverse
cumulative frequency, reverse cumulative percent and valid percent
variables listed and they should occur in the same order as those
variables are listed. Text containing spaces should be bound in " ".
Thus with show(f RF), showhead(# "# bigger") specifies
that frequencies are indicated by "#" and the reverse
cumulative frequencies are indicated by "# bigger".

colorder() specifies a reordering of what would otherwise be shown
as the columns of the listing. You may specify one or more positive
integers. Suppose groups would show 4 columns, but you want the
third and fourth columns to be shown first (i.e. as the leftmost
columns) and then the first and second columns. colorder(3 4 1 2)
or just colorder(3 4) would specify that. (It follows that this
option will not omit columns, although it may be used to repeat columns.)
Typically therefore this option is used on a second or later pass of
groups.

saving results

saving(filename [, save_options]) save results to dataset

saving() specifies that the results listed will be saved to a
named Stata .dta file using save. That does not include any sums,
means or similar summaries. Options of save may be specified in
the usual way. This option may not be combined with by:.

Examples

. sysuse auto, clear

. groups foreign

. tabulate foreign

. groups foreign rep78

. tabulate foreign rep78

. groups foreign rep78, fillin

. groups foreign rep78, fillin saving(mytable, replace)

. bysort foreign: groups rep78

. groups foreign rep78, percent(foreign)

. groups foreign rep78, percent(foreign) show(f p P)

. groups mpg, select(f == 1) show(none)

. groups mpg, select(5)

. groups mpg, select(-5)

. groups mpg, select(5) order(h)

. groups foreign rep78, fill select(f == 0) show(none)

. groups foreign rep78, sepby(foreign)

. groups foreign rep78, sepby(foreign) showhead(# %)

. groups rep78, missing show(freq percent vpercent) sep(0)

. groups rep78, show(freq rfreq Rpercent) ge

. groups rep78, show(F f Rf) lt showhead(< = >)

. groups mpg, reverse

. groups mpg, reverse show(f p RP) ge

. webuse nlswork

. groups collgrad not_smsa c_city south, order(high) sep(0)

. groups collgrad not_smsa c_city south, order(high) sep(0) colorder(5 6)

Author

Nicholas J. Cox, Durham University, U.K.

n.j.cox@durham.ac.uk

Acknowledgments

Fred Wolfe made very helpful comments. He, Roger Harbord and Eric Zbinden
all found a bug. A question from Stefan Gawrich led to the ge option.
A question from James Keeler led to the reverse option.
A question from William Parry led to the saving() option.

Also see

Online: help for
tabulate,
table,
list;
duplicates,
contract,
modes (if installed),
fre (Ben Jann; if installed),
qplot (if installed),
distplot (if installed)