help sortobs
-------------------------------------------------------------------------------
Title

sortobs -- Sort observations into a specified order.

Syntax

Sort observations according to a variable's specific values

sortobs varname , values(stringlist) [first last before(string) after(string)]

Sort observations by observation numbers

sortobs , values(numlist) [first last before(#) after(#)]

where

stringlist is a list of (potentially quoted) strings separated by spaces, and

numlist is a list of numbers with blanks or commas in between (see [U] numlist for details).

Description

sortobs relocates observations by specifying either (1) a variable's specific values or (2) observation numbers. Observations that are not specified in the command retain their original, respective sort orders.

Options

values() specifies the observations to be relocated. They are relocated in the order they are specified. The user can specify the observations by observation number (see example 1) or by observation contents (see example 2).

first shifts the specified observations to the beginning of the dataset. This is the default.

last shifts the specified observations to the end of the dataset.

before() shifts the specified observations before a particular row. The row can be specified by observation number or observation contents.

after() shifts the specified observations after a particular row. The row can be specified by observation number or observation contents (see example 3).

Examples

1. Reverse the order of the first three observations:

. sysuse auto.dta, clear

. list in 1/5

. sortobs, values(3 2 1)

. list in 1/5

2. Shift two observations to the end of the dataset:

. sysuse auto.dta, clear

. keep in 1/5

. list in 1/5

. sortobs make, values("AMC Pacer" "AMC Spirit") last

. list in 1/5

3. Move an observation to the middle of the dataset

. sysuse auto.dta, clear

. keep in 1/5

. list in 1/5

. sortobs make, values("Buick Electra") after("AMC Pacer")

. list in 1/5

Author

Julian Reif, University of Chicago

jreif@uchicago.edu

Also see

sort, gsort