List group frequencies
groups varlist [weight] [if exp] [in range] [, fillin format(format) ge lt missing order({high|low}) select({condition|#}) show(frequencies_to_show) showhead(header_text_for_frequencies) reverse list_options saving(filename[, save_options]) ]
by ...: may be used with groups; see help by. Note in particular that this is the 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. 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
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.
format() specifies a numeric format for percent and cumulative percent frequencies. The default is %6.2f.
ge 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 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.
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.
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 element 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, 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 unnecessary, while the percents and cumulative percents may not be of interest, so show(none) may be desired.
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".
reverse reverses what would otherwise be displayed, last values first.
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.
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
. groups foreign . groups foreign rep78 . groups foreign rep78, fillin . groups foreign rep78, fillin saving(mytable, replace)
. bysort foreign: groups rep78, ord(h) N
. groups mpg, sel(f == 1) show(none) . groups mpg, sel(5) . groups mpg, sel(-5) . groups mpg, sel(5) ord(h)
. groups foreign rep78, fill sel(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
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)