-------------------------------------------------------------------------------
help for rowsort
-------------------------------------------------------------------------------

Row sort a set of integer variables

rowsort varlist [if exp] [in range] , generate(newvarlist) [ ascend descend missing(#) ]

Description

rowsort creates new variables containing the sorted (ordered) values in each observation of varlist. varlist should contain only numeric variables with integer values.

Missing values are allowed. By default they are sorted to arbitrarily high values, as is standard in Stata. This may not be what you want, so see the documentation of the missing() option.

By default, the first (second, ...) new variable contains the lowest (second lowest, ...) value within each observation. The descend option may be used to reverse order.

Remarks

rowsort loops over observations and may be relatively slow. It may be faster to reshape, sort within blocks, and reshape again.

Options

generate() specifies new variable names for the variables to be generated, one for each variable in varlist. newvarlist may be specified in hyphenated form, as in s1-s5. This option is required.

ascend specifies that newvarlist should contain ascending values and is the default.

descend specifies that newvarlist should contain descending values, such that ordering is from largest downwards.

missing() specifies that missing values should be treated as the number specified while sorting. Typically, missing() specifies some negative value that does not occur in the data, to ensure that missings are sorted to the end. Thus given 1 . 3 . 5 . 7, rowsort by default sorts these to 1 3 5 7 . . ., which is fine if you want to read off the lowest, next lowest and so forth. But with the descend option, rowsort would sort to . . . 7 5 3 1, which makes it difficult to read off the highest, next highest, and so forth. With missing(-999), say, the set would be treated as 1 -999 3 -999 5 -999 7, sorted to 7 5 3 1 -999 -999 -999 and reported as 7 5 3 1 . . .. Note that there is no check of whether the number specified occurs in the data and that no attempt to made to distinguish different types of missing data. For other ways of handling this kind of problem, see mvencode.

Examples

. rowsort x1-x5, gen(s1-s5) . rowsort x1-x5, gen(s1-s5) descend

Author

Nicholas J. Cox, Durham University n.j.cox@durham.ac.uk

Acknowledgements

A problem raised by Alice Dobson led to the missing() option.