Re: st: RE: Creating interaction terms of 30 continuous variables

 From Nick Winter <[email protected]> To [email protected] Subject Re: st: RE: Creating interaction terms of 30 continuous variables Date Thu, 10 Sep 2009 15:53:25 -0400

```Leaving aside whether this is wise, I'd do something like this:

unab vars : var1-var30
local nvar : word count `vars'
forval i = 1/`nvar' {
forval j = 1/`=`i'-1' {
local x : word `i' of `vars'
local y : word `j' of `vars'
generate `x'X`y' = `x' * `y'
}
}

```
This puts the variable list into local macro `vars', then grabs the variables sequentially from the list. The inner loop (y) only goes up to the variable prior to the current outer loop (x) variable, which means we create only one version of each interaction and avoid creating the squares of each variable as well.
```
```
Or, in Stata 11, you can specify all the interactions with factor varlists, without creating any interaction variables at all. For example, to regress variable myvar on all the interactions:
```
regress myvar c.(var1-var30)#c.(var1-var30)

- Nick Winter

```
```
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jean-Gael
Collomb
Sent: Wednesday, September 09, 2009 8:13 AM
To: [email protected]
Subject: st: Creating interaction terms of 30 continuous variables

Hello,
```
I am trying to explore all possible interaction terms in my dataset and I am not sure how to do this efficiently using stata. I have about 30 continuous variables. It seems that xi is limited to non continuous variables and xi3 does not seem to work for 30 variables, unless I am doing something wrong. I came up with a solution using "foreach" to generate all the interactions terms and then try them in a regression model:
```
foreach x of varlist var1-var30 {
foreach y of varlist var1-var30 {
generate `x'X`y'=`x'*`y'
}
}

```
The first problem I have is that this created duplicates (i.e. var1Xvar2 is the same as var2Xvar1). Furthermore, I feel it is a cumbersome way to do it, and I wonder if there is a more efficient way to generate all possible pair interaction terms or better yet to have an exploratory regression models testing all these pairs out and selecting the best model. I saw someone someone doing something similar very quickly in NCSS and I was hoping I could do the same in STATA.
```Thanks for your feedback.

Jean-Gael "JG" E. Collomb
PhD candidate
```
School of Natural Resources and Environment / School of Forest Resources and Conservation
```University of Florida
[email protected]
[email protected]
+1 (352) 870 6696

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```
--
--------------------------------------------------------------
Nicholas Winter                                 434.924.6994 t
Assistant Professor                             434.924.3359 f
Department of Politics                  [email protected] e
University of Virginia          faculty.virginia.edu/nwinter w
PO Box 400787, 100 Cabell Hall
Charlottesville, VA 22904

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```