Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Creating interaction and square variables


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Creating interaction and square variables
Date   Fri, 31 Aug 2012 02:20:54 +0100

It seems that you are not getting the point.

In the case of

 gen f`i'f`j'=firm`i'*firm`j' if `i'<=`j'

a variable will always be created. However, the qualifier

... if `i' <= `j'

will mean that _all_  its values will be missing if that condition

`i' <= `j'

is not true. That is pointless.

Consider any i and j pair for concreteness, say i = 7, j = 3 or i = 3,
j = 7. The result of the -if- qualifier being evaluated is the same
regardless of which observation is being considered. That is why, as
Dimitriy explained, the appropriate check is to use the -if- command,
not the -if- qualifier. That way, you get one test, not several.

However, -drop-ping variables you don't want is not needed here at
all. Just ensure that they are not created in the first place. You
don't need any kind of -if- to do this. For example,

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

It may help to think about this in matrix terms. You want one half of
a matrix, and its principal diagonal.

Nick

On Fri, Aug 31, 2012 at 1:49 AM, Mike Kim <[email protected]> wrote:
> 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: [email protected]
> [mailto:[email protected]] On Behalf Of Dimitriy V.
> Masterov
> Sent: Thursday, August 30, 2012 7:31 PM
> To: [email protected]
> 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 <[email protected]> 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?
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index