help myaxis

Title

myaxis -- Reorder categorical variable by specified sort criterion

Syntax

myaxis newvar=varname
[if] [in]
,
sort(criterion)
[
subset(true_or_false_condition)
missing
descending
varlabel(string)
valuelabelname(string)
]

Description

myaxis maps an existing "categorical" variable, meaning
usually a numeric variable with integer codes and value labels, or
equivalently a string variable, to a new variable with integer values 1
up and with value labels, sorted according to a specified criterion.

Remarks

The command name myaxis is to be parsed "my axis". The
second element "axis" arises from a leading application of the command.
You have a categorical variable that would define an axis of a graph, or
one dimension of a table (the rows, or the columns, say), but the
existing order of categories is not ideal. Some graph and table commands
offer sorting on the fly, but this command may help wherever other
commands do not offer that.

The first element "my" is at best harmless whimsy, but arises
because a command named just axis would be harder to spot among
other uses of the term. If you find it irritating or annoying, clone and
rename the command. Now it's yours, modulo your use of my work.

The problem is split by myaxis into these parts:

1. Calculation of a numeric variable on which to sort
categories. myaxis treats this as an application of egen.
Note: If a variable already exists that defines the sort order and is
constant within categories, then asking for (say) its minimum, mean, or
maximum within each category will suffice.

2. Deciding whether you want ascending order (the default) or
descending order (highest value goes first). Descending order requires
negation of the variable from #1.

3. Mapping your categorical variable to integers 1 up. The
group() function of egen does the work here, but
myaxis is careful to split ties according to the original
variable. (For example: suppose nominal categories A, B, C, D, E have
frequencies 7, 7, 42, 3, 1 and you want them sorted by frequency. You
don't want A and B lumped together because they have the same
frequency.)

4. Fixing a variable label. myaxis uses a new variable
label if supplied; otherwise, the original variable label; and, if that
does not exist, the original variable name.

5. Fixing value labels. This is even more important than #4 for
helpful display in a graph or table.
myaxis uses the original value labels if defined and otherwise the
original string or numeric values.

Options

sort() specifies the criterion for sorting. It is a
required option. The criterion should always include the name of an
egen function. The function may be community-contributed so long
as the code is visible along your adopath. The criterion may also
include the name of an existing variable and that is essential whenever
the sort criterion is not based on varname.

subset() specifies a subset of the data on which the sort
criterion should be calculated. Concretely, imagine two variables that
define y and x axes of a graph or rows and columns of
a table. You might want rows to be sorted by values calculated for a particular
column, or columns to be sorted by values calculated for a particular
row.

missing specifies that missing values of varname are to be
included. The default is to ignore them.

descending specifies sorting with highest value first.
The default sort order is ascending, with lowest value first.

varlabel() specifies a variable label for the new
variable. Otherwise see #4 in the Remarks.

valuelabelname() specifies a new value label name for the
value labels of the new variable. This will be needed if there is
already a set of value labels called newvar.

Examples

. sysuse auto, clear

. myaxis wanted=rep78, sort(count) descending

. tab wanted

. tab wanted, nola

. myaxis wanted2=rep78, sort(mean mpg) descending

. format mpg %2.1f

. tab wanted2, su(mpg)

. myaxis wanted3=rep78, sort(mean mpg) subset(foreign==1) descending

. tab wanted3 foreign , su(mpg) nost nofreq

. webuse nlsw88, clear

. myaxis wanted=industry, sort(median wage) descending

. tabstat wage, s(median mean) by(wanted) format(%3.2f)

Author

Nicholas J. Cox, Durham University, UK

n.j.cox@durham.ac.uk

Also see

Online:
[D] egen,
[R] tabulate_oneway,
[R] tabulate_twoway,
[G-2] graph_dot,
[G-2] graph_bar,
labmask (Stata Journal; if installed),
egenmore (SSC; if installed),
tabplot (Stata Journal; if installed),
stripplot (SSC; if installed)