Bookmark and Share

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

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

Re: st: foreach for interaction term

From   Neil Shephard <>
Subject   Re: st: foreach for interaction term
Date   Tue, 24 May 2011 15:23:43 +0100

On Tue, May 24, 2011 at 3:21 PM, Barbara Engels <> wrote:
> well, now i modified it again, to make it more understandable for me and for stata.
>  foreach `x' of ger_ims bel_ims chin_ims usa_ims uk_ims it_ims switz_ims {
>  2.   ge foreignbrd*=`X'*(*brd)
>  3.  }
> invalid syntax
> r(198);
> I want it to create values containing the product of import share and R&D stock (=foreignbrd) of each country for each year.
> Is that understandable? I 'm a little confused.

You've misunderstood how to use loops, you don't put ` and ' around
the local you wish to define to be used in the loop, nor can you
change the case within your loop.  Legal syntax for the above would

Break this problem down, you have multiple countries and for each you
have two variables.  You want to construct a new variable for each
country that is the product of each of the others.

Thus I would start by...

1.  Looping over each of the countries.
2. For each country I'm looping over generate the interaction term

So something like...

foreach x of ger bel chin usa uk it switz {
  gen interaction_`x' = `x'_ims * `x'brd

Although if you're using Stata >11.0 (which you haven't told us, see
comments in the Statalist FAQ) then you might want to look at the -man
fvvarlist- for a much easier way of using interactions in subsequent
analyses you wish to perform.


“Truth in science can be defined as the working hypothesis best suited
to open the way to the next better one.” - Konrad Lorenz

Email -
Website -
Photos -

*   For searches and help try:

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