Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • foreach with the same variable but with interactions

    Hello,
    I'm trying to iterate over a regression changing a single variable for its self-interactions like this:

    HTML Code:
    foreach var of varlist c.myVar  c.myVar#c.myVar c.myVar##c.myVar  c.myVar#c.myVar#c.myVar c.myVar##c.myVar##c.myVar {
    reg y $controls `var', r
    }
    But I get an error message:
    HTML Code:
    c:  operator invalid
    I need the c operator in order to set the interactions. I think this would be very straightforward for an experienced Stata person.
    Can anybody give me an idea on how to address this?
    Thank you so much in advance.

  • #2
    The problem is that when you include the c., #, and ## operators, what you have is no longer the name of a variable, and only variable names can appear in a varlist. Try this instead:
    Code:
    foreach var in c.myVar c.myVar#c.myVar c.myVar##c.myVar c.myVar#c.myVar#c.myVar c.myVar##c.myVar##c.myVar {
        reg y $controls `var', r
    }

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      The problem is that when you include the c., #, and ## operators, what you have is no longer the name of a variable, and only variable names can appear in a varlist. Try this instead:
      Code:
      foreach var in c.myVar c.myVar#c.myVar c.myVar##c.myVar c.myVar#c.myVar#c.myVar c.myVar##c.myVar##c.myVar {
      reg y $controls `var', r
      }
      It worked perfectly. Thank you so much!

      Comment

      Working...
      X