Sort & order variables
Sorting and ordering variables
sysuse
census.dta, clear
// load the pre-installed dataset "census"
browse
state region pop
// let's look at the variables state, region & pop
* sorting
sort
pop
// sort w.r.t. pop in ascending order
sort
region pop
// first sorts by region number and then by pop
gsort
-pop
// descending order
// sort can be important when creating variables
* ordering variables
order
region state
// pop as first variable, state second
// order is important for varlists:
summarize
poplt5-pop65p
order
pop18p
summarize
poplt5-pop65p
Exercise
Load the pre-installed dataset auto.
- Sort the data such that you can answer the following questions by a brief look at the data: Which is the cheapest of the domestic cars, which of the foreign cars? Which is the most expensive of the domestic cars, which of the foreign cars?
- Sort the data such that it is sorted by car type (foreign first, then domestic), repair records (lowest first), and price (highest first).
- Order the variables such that the first variable remains "make", but the second variable becomes "foreign"
- Order the variables alphabetically (Hint: Look at the options of "order" in the help file. You might abbreviate all variables by typing "*").