The purpose of the inlist command in Stata is to determine if a particular argument belongs to a specific list. It allows the user to use multiple conditions or multiple values in the condition. Let’s explain this with the help of an example. We will use the example dataset provided by Stata itself. Use the below command to open the dataset
Download Example Filesysuse auto.dta, clear
Let’s assume that we want to summarize the three variables, that are price, mpg, and weight, but for a specific type of car. The dataset comprises several cars, but our focus is only on a few, which include the AMC Concord, AMC Pacer, AMC Spirit, Buick Century, Buick Electra, Buick LeSabre, Buick Opel, and Buick Regal. This can be done with or without using the inlist function in Stata.
Related Article: Stata Command Modifiers if, in, by, bysort Qualifiers and Statements in Stata
The syntax without the inlist function is given below:
summarize price mpg weight if make == "AMC Concord" | make == "AMC Pacer" | make == "AMC Spirit" | make == "Buick Century" | make == "Buick Electra" | make == "Buick LeSabre" | make == "Buick Opel" | make == “Buick Regal"
In the above command, firstly, we will write the command summarize, the variable name we want to summarize, then if qualifier, and then we add the argument. But each time we add a new argument, the variable name must be specified as done above. In the above syntax, “make” is the variable, and AMC Concord, AMC Pacer, AMC Spirit, Buick Century, Buick Electra, Buick LeSabre, Buick Opel, and Buick Regal are the arguments.
The above analysis can be done better by using Stata’s built-in inlist
function in Stata. The built-in inlist function is more convenient, short and fast. The syntax with the inlist function is given below:
summarize price mpg weight if inlist(make, "AMC Concord", "AMC Pacer", "AMC Spirit" , "Buick Century", "Buick Electra", "Buick LeSabre", "Buick Opel" , "Buick Regal")
Rather than repeating the variable name before each argument, we write the inlist command after, if qualifier, in parentheses, first, we will write the variable name, commas, and then all the arguments we want to focus on.
The inlist command in Stata can be used for both the string and numerical values
Let’s replace the variable “make”, which was the string, with the rep78, a numerical variable, to summarize the price, mpg, and weight.
summarize price mpg weight if inlist(rep78, 1, 2, 3)
Remember that we can’t give the range, greater or less than, and equal signs within the inlist function; only values can be given.
The above table represents the descriptive statistics of price, mpg and weight only for 1, 2 and 3 values of rep78.
Limitation of inlist Function in Stata
There are some limitations of the inlist function. In Stata while using inlist, we can only provide 10 arguments for the string, whereas 250 arguments for the real numbers. The problem arises when we have more than 10 string arguments. This can be resolved using the inlist function twice.
Let’s assume that we want to summarize more than 10 arguments; the syntax we will use for that is given below:
summarize price mpg weight if inlist(make, "AMC Concord", "AMC Pacer", "AMC Spirit" , "Buick Century", "Buick Electra", "Buick LeSabre", "Buick Opel" , "Buick Regal", "Buick Riviera", "Buick Skylark", "Cad. Deville")
On running the above syntax, the following error will appear:
To resolve it, we will add the inlist function once again. After the nine arguments, we will add a second inlist function, and the variable name and arguments. But before that, do not forget to add | (or sign)
summarize price mpg weight if inlist(make, "AMC Concord", "AMC Pacer", "AMC Spirit" , "Buick Century", "Buick Electra", "Buick LeSabre", "Buick Opel" , "Buick Regal") | inlist( make, "Buick Riviera", "Buick Skylark", "Cad. Deville")
The second limitation of the inlist function in Stata is that arguments should either be string or numerical. The mix of string and numerical argument will give the error. If any variable has numeric and string, then numeric should also be written in inverted commas, as presented below:
summarize price mpg weight if inlist(make, "1", "AMC Pacer", "AMC Spirit")
Related Article: Using loop in Stata
Not inlist
Suppose we want to summarize price, mpg and weight on the base of all the arguments of variable make except for two. Then, in that case, we will use the !inlist function. The syntax is given below
summarize price mpg weight if !inlist(make, "AMC Pacer", "AMC Spirit")
The above syntax has excluded the AMC Pacer and AMC Spirit.
Inlist2 Command in Stata
One common problem faced by Stata users is that each time they work with a string variable, they have to write the argument in the quotation mark. This problem can be solved by using the inlist2 command. There are so many benefits of the inlist2 command in Stata. Unlike the inlist function, string values in the inlist2 command do not need to be enclosed arguments in quotation marks, making them quicker to write. There is no limit of 10 arguments for strings or 250 arguments for reals in the inlist2 command.
The inlist2 function is user-written; therefore, it is a must to install it first. Use the below syntax to install the inlist2 function.
ssc install inlist2
The inlist2 creates the dummy variable. It assigns value 1 where the condition is met and 0 where the condition is not. The syntax is given below:
inlist2 make, values (AMC Concord, AMC Pacer, AMC Spirit, Buick Century, Buick Electra, Buick LeSabre, Buick Opel, Buick Regal, Buick Riviera, Buick Skylark, Cad. Deville)
When we run this command, the dummy variable will be generated.
The limitation of inlist2 is that it does not allow space within the commas. This is because this space considers part of the text. The dummy variable can also be named using the syntax below.
inlist2 make, values (AMC Concord, AMC Pacer, AMC Spirit, Buick Century, Buick Electra, Buick LeSabre, Buick Opel, Buick Regal, Buick Riviera, Buick Skylark, Cad. Deville) name(aaa)
Now we will summarize the price, mpg and weight using the below syntax:
Summarize price mpg weight if inlist2 == 1