List command in Stata

June 08, 2019

The list command is one of the most useful commands in Stata when inspecting data. In this post we’ll look at some ways we can list the values of one variable based on the value of another. In order to do so we’ll use the list command in Stata with an if condition:

list var1 if var2 == "value"
list var1 if var2 == "value" in 1/10

Here, var1 is the variable we want to list, filtered on some value in var2. When dealing with a large dataset it might be useful to apply the second variation which limits the number of results to the 10 first that meets the condition specified.

sysuse auto, clear
list mpg if make == "AMC Concord"

To apply case-insensitive matching, we use the strupper() or strlower() functions:

list mpg if strupper(make) == "BUICK ELECTRA"

or

list mpg if strlower(make) == "buick electra"

A common use-case is to filter on a substring, for this purpose we can use the strpos() function:

list make mpg price if strpos(make, "Buick") > 0

To list a variable based on multiple values of another variable, either use the inlist() function:

 list make price if inlist(make, "Buick Electra", "Buick LeSabre", "Buick Riviera")

Or logical operators:

list make price if make == "Buick Electra" | make == "Buick LeSabre" | make == "Buick Riviera"

Those were a few different ways of using the list command, we’ll explore other variations in another post.


Profile picture

Written by Johan Osterberg who lives and works in Gothenburg, Sweden as a developer specialized in e-commerce. Connect with me on Linkedin

2024 © Johan Osterberg