- Laura Whiting

# Combinations of Variables – Run an Estimation Command on a List of Variables

Updated: Jun 22, 2020

This post covers how to run through a list of variables and pair the variables up in all the available combinations. For example, I have a list of variables “var1 var2 var3”. I want to run several linear regressions on all combinations of variables in that list. For a small list like this I can do that manually by running 3 regressions, one for var1 var2, the second for var1 var3, and the third for var2 var3. However, if I have a long list of variables this becomes tedious. I want to automate the process to make Stata do all the work of pairing up variables, running the regressions and saving the results. To do this I make use of Stata *local macros* and the **forvalues{} **and **foreach{}** loops. I will also save the list of paired variables to a separate string variable.

**Worked Example**

This example makes use of the Stata example dataset auto.dta. This can be loaded into Stata using the command **sysuse auto**. I am going to pair up the variables *price*, *mpg*, *rep78*, *headroom*, and *trunk*. I use the calculation *(number_of_variables*number_of_variables-1)/2* to get the number of combinations I need to loop through. In the command pane I type the following:

The last two lines in the code above give the following output:

Here you can see the list of variable combinations saved to the variable “levels”, and a list of the regressions stored using **estimates store**.

It is worth noting you can modify the code shown above to allow this to work with any number of combinations (for example - 3 or more variables). You can also modify this code to rotate through a list of variables, trying each one out as the dependent variable.

If you would like to know more about programming in Stata, check out __An Introduction to Programming in Stata__. This is a fabulous introduction into the world of programming and automation in Stata.