------------------------------------------------------------------------------- help forrowsort-------------------------------------------------------------------------------

Row sort a set of integer variables

rowsortvarlist[ifexp] [inrange],generate(newvarlist)[ascenddescendmissing(#)]

Description

rowsortcreates new variables containing the sorted (ordered) values in each observation ofvarlist.varlistshould 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

descendoption may be used to reverse order.

Remarks

rowsortloops over observations and may be relatively slow. It may be faster to reshape,sortwithin blocks, andreshapeagain.

Options

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

ascendspecifies thatnewvarlistshould contain ascending values and is the default.

descendspecifies thatnewvarlistshould 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 given1 . 3 . 5 . 7,rowsortby default sorts these to1 3 5 7 . . ., which is fine if you want to read off the lowest, next lowest and so forth. But with thedescendoption,rowsortwould sort to. . . 7 5 3 1, which makes it difficult to read off the highest, next highest, and so forth. Withmissing(-999), say, the set would be treated as1 -999 3 -999 5 -999 7, sorted to7 5 3 1-999 -999 -999and reported as7 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

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

AcknowledgementsA problem raised by Alice Dobson led to the

missing()option.