help gsort dialog: gsort
-------------------------------------------------------------------------------
Title
[D] gsort -- Ascending and descending sort
Syntax
gsort [+|-] varname [[+|-] varname ...] [, generate(newvar) mfirst]
Menu
Data > Sort > Ascending and descending sort
Description
gsort arranges observations to be in ascending or descending order of the
specified variables and so differs from sort in that sort produces
ascending-order arrangements only; see [D] sort.
Each varname can be numeric or a string.
The observations are placed in ascending order of varname if + or nothing
is typed in front of the name and are placed in descending order if - is
typed.
Options
generate(newvar) creates newvar containing 1, 2, 3, ... for each group
denoted by the ordered data. This is useful when using the ordering
in a subsequent by operation.
mfirst specifies that missing values be placed first in descending
orderings rather than last.
Examples
---------------------------------------------------------------------------
Setup
. sysuse auto
Place observations in ascending order of price
. gsort price
Same as above command
. gsort +price
List the 10 lowest-priced cars in the data
. list make price in 1/10
Place observations in descending order of price
. gsort -price
List the 10 highest-priced cars in the data
. list make price in 1/10
Place observations in alphabetical order of make
. gsort make
List make in alphabetical order
. list make
Place observations in reverse alphabetical order of make
. gsort -make
List make in reverse alphabetical order
. list make
---------------------------------------------------------------------------
Setup
. webuse bp3
Place observations in ascending order of time within ascending order of
id
. gsort id time
List each patient's blood pressures in the order measurements were taken
. list id time bp
Place observations in descending order of time within ascending order of
id
. gsort id -time
List each patient's blood pressures in reverse-time order
. list id time bp
---------------------------------------------------------------------------
Also see
Manual: [D] gsort
Help: [D] sort