Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# RE: st: Creating interaction and square variables

 From "Mike Kim" To Subject RE: st: Creating interaction and square variables Date Thu, 30 Aug 2012 19:49:08 -0500

```Thanks, Dimitriy.

I changed the code like the following and it works. I am still curious
because the first "if" works well. I am using Stata 12.

forv i=1/22 {
forv j=1/22 {
gen f`i'f`j'=firm`i'*firm`j' if `i'<=`j'
}
}

forv i=1/22 {
forv j=1/22 {
if `i' >`j' {
drop f`i'f`j'
}
}
}

Mike.

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Dimitriy V.
Masterov
Sent: Thursday, August 30, 2012 7:31 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Creating interaction and square variables

Mike,

You are not understanding how if works. Take a look at this:
http://www.stata.com/help.cgi?ifcmd

Depending on which version of Stata you have, you also might want to look
into factor variable notation to do this for you on the fly.

DVM

On Thu, Aug 30, 2012 at 3:39 PM, Mike Kim <kalisperos@gmail.com> wrote:
> Hi all,
>
> I would like to create interaction and square variables. Let say, I
> have three variables x1, x2, x3.
>
> I used to the following code to create interactions and square of x1,
> x2, x3.
>
> forv i=1/3 {
>   forv j=1/3 {
>   gen x`i'x`j'=x`i'*x`j'    //  if `i'<=`j'   : This still creates
variables
> with missing values.
>   }
> }
>
> Since x1*x2 = x2*x1, for example, I tried to drop those same variables
> using the following code, but got error message: "invalid syntax, r(198)".
>
> forv i=1/3 {
>  forv j=1/3 {
>  drop x`i'x`j' if  `i'<`j'
>  }
> }
>
> Can you please let me know what is wrong in the code? Thank you for
>
> Mike.
>
> *
> *   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/

*
*   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/
```