Arranging Your Data

sort; gsort

sort/gsort arranges the observations in ascending order.
. sysuse auto
. sort make

gsort -var can arrange the observations in descending order.

. gsort mpg sorts mpg in ascending order.
. gsort -mpg price sorts mpg in descending order and price in ascending order.

order

order arranges the order of variables.

order varlist(, first) or order varlist, last moves the variables to the first or last column.
. order trunk turn, last moves the two variables trunk and turn to the last two columns.

order varlist, before(var) or order varlist, after(var) moves the variables before or after a certain variable.
. order trunk turn, before(mpg) moves trunk and turn before mpg.

Author: Yun Dai, 2018