Announcement

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

  • How to use labels of variables in loop

    Hi,

    I want to create a table with my estimation results. Moreover, I want to have the label of the dependent variable as column title. See the following code:

    local y "wage income"

    foreach var in y{
    ivreg2 `y' age age_sq educ, cluster(id)
    est store olscomp`y', title(`:label variable `y'' sample1)
    }


    estout olscompwage olscompincome, label


    This code returns only invalid syntax. Do you have any clue? Any help is highly appreciated.


    Daniel


  • #2
    Do look at the syntax of foreach again. My guess is that you don't want either of the references you are asking for.

    The tell-tale problems are (1) defining a loop macro you never use inside the loop (not illegal, but usually wrong) and (2) using the in syntax of foreach when you need the of syntax.

    Code:
    local y "wage income"
    
    foreach var of local y {
        ivreg2 `var' age age_sq educ, cluster(id)
        est store olscomp`var', title("`:label variable `var'' sample1")
    }

    Comment


    • #3
      Daniel,

      I agree with Stata, your code has invalid syntax. Read more help on how to use loops and refer to local variables.
      The problem of your code that although you create a loop over the items in y, you keep on referring to y as a whole, instead of using the specific item in the body of the loop.

      Compare to the following example:

      Code:
      clear
      sysuse auto
      
      local y "mpg rep78 trunk"
      
      foreach var in `y' {
        display `"Variable is: `var'"'
        display `"Label is: `:variable label `var''"'
        tabulate `var'
      }
      Best, Sergiy


      Comment


      • #4
        Your local macro y is defined as containing two variable names: wage and income. When you invoke -`:label variable `y''-, Stata sees -`:label variable wage income'-, which is a syntax error because the :label variable macro extended function allows only one variable to be named. I notice that you are also going through the loop twice (once for wage and once for income) but you do the same regression -ivreg2 wage income age age_sq educ, cluster(id)- both times. You are also attempting to store those results in the same place both times: as olscompwage income, which, by the way is also a syntax error because blank spaces are not permitted within the name.

        I think you have simply coded your loop incorrectly. It should be:

        Code:
        local y "wage income"
        
        foreach var in y{
            ivreg2 `var' age age_sq educ, cluster(id)
            est store olscomp`var', title(`:label variable `var'' sample1)
        }
        Added: Crossed with #2 and #3, which both make the same points.

        Comment


        • #5
          There are similar guesses in #2 #3 #4 on what you want, but

          Code:
          foreach var in y
          is a typo in #4. See either #2 or #3 on that particular detail.

          Comment


          • #6
            Oops, yes, sorry! That should be -foreach var in `y'-. Or, another way, which I'm more inclined to use myself, is -foreach var of local y- (The absence of quotes in this last construction is intentional and correct.)

            Comment


            • #7
              Thanks for your help!

              Comment

              Working...
              X