For this tutorial we are going to use the
auto dataset that comes with Stata. To load this data type
sysuse auto, clear
The auto dataset has the following variables.
Suppose we want to get some summarize statistics for price such as the mean, standard deviation, and range. We’ll use the
Now let’s add the option
summarize. This will give us lots more information, including the median and other percentiles.
summarize price, detail
Multiple Variables at Once
To get descriptives for multiple variables at once just add the variable names after
summarize price mpg
summarize price mpg, detail
Suppose we want to get the descriptive statistics for price by car type (foreign vs domestic). We can use what is called
by foreign: summarize price
When using the by command, the variable of interest needs to be sorted in the data set. For example, in the previous example the variable “foreign” is already sorted within our data set. If we wanted to examine the price by mpg, we would need to sort miles per gallon. One way to sort data is using a simple sort command followed by the variable name. Stata will sort the data in ascending order by default.
After we sort the data, we can then use the standard by mpg: command. In
by processing, we can also sort the data and execute the
by command at the same time using the
bysort mpg: summarize price
by command can also be used in other commands, such as creating graphics. For example, if we wanted to examine histograms of mpg by the make of the car, we would use the
by command as an option. The make of car does not have to be sorted for this command.
by statement will give us descriptives for all levels of the
by variable (i.e., both foreign and domestic). Suppose we just want the describes for one level of the
by variable. We can use the
if statement for that. For foreign cars (i.e.,
foreign == 1):
summarize price if foreign == 1
For domestic cars (i.e.,
foreign == 0)
summarize price if foreign == 0
This table is to help in determining how to specify what levels of the variable you want to use.
|==||is or is equal to|
|!= or ~=||is not or is not equal to|
|>||is greater than|
|>=||is greater than or equal to|
|<||is less than|
|<=||is less than or equal to|
|*From pg. 74 of A Gentle Introduction to Stata by Alan Acock|
in qualifier specifies a particular subset of cases based on their order in the dataset. For example, if we want to examine the mpg in the 10 least expensive cars, we would use the
sort price summarize mpg in 1/10
As a helpful hint for any of these processes, if your variables are labeled (showing the label instead of the numeric value) and you need to find the numeric values to examine levels of the variable, you can use the
This will show you the number values for variables. You can also find those values by double-clicking on them in the data browser.