Announcement

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

  • in a -foreach v- loop, reference -v- and "-v+1-"

    In this example, I have four variables. I want to run some regressions on them and display the regression on the first two variables together, as well as those on the third and fourth variable.

    Code:
    clear
    sysuse auto.dta
    
    scalar i = 1
    
    di i
    
    foreach v in mpg rep78 headroom weight {
        eststo: quietly: reg `v' price trunk length
        eststo: quietly: reg `v' price trunk length, vce(robust)
        /*
        eststo: quietly: reg `w' price trunk length
        eststo: quietly: reg `w' price trunk length, vce(robust)
        */
        esttab * using esttab-`=scalar(i)'.tex,
      
        scalar i = i+1
    }
    In the above, I use w as a placeholder. That part is commented out using /**/ because it would not work.

    What I want is to use a loop to simplify something of the following structure:


    Code:
      
        eststo: quietly: reg mpg price trunk length
        eststo: quietly: reg mpg price trunk length, vce(robust)
      
        eststo: quietly: reg rep78 price trunk length
        eststo: quietly: reg rep78 price trunk length, vce(robust)
      
        esttab * using esttab-1.tex
    
        eststo: quietly: reg headroom price trunk length
        eststo: quietly: reg headroom price trunk length, vce(robust)
      
        eststo: quietly: reg weight price trunk length
        eststo: quietly: reg weight price trunk length, vce(robust)
      
        esttab * using esttab-2.tex

    So what I want to do running through the above foreach loop only twice, and cover two of the four variables each time.

    In other software, I would define an array consisting of mpg rep78 headroom weight, and taking two elements with each iteration of a loop that has only two iterations. But how can I do that, or something else with the same result, in Stata?

  • #2
    Hello Max,

    Here's one way you can do it. I've just provided the looping structure as proof of concept. Hopefully this make sense.

    Code:
    foreach v in "mpg rep78" "headroom weight" { //Use quotations to make "arrays"
        tokenize `v' //This breaks up the "array" by whitespace into different consecutively numbered macros
        di "First: `1' | Second: `2'" //`1' and `2' would be your variables for each iteration
    }

    Comment


    • #3
      Thank you very much. This worked.

      The solution, applied to my problem, looks as follows:

      Code:
      clear
      sysuse auto.dta
      
      scalar i = 1
      
      foreach v in "mpg rep78" "headroom weight" {
      
      tokenize `v'
      
          eststo: quietly: reg `1' price trunk length
          eststo: quietly: reg `1' price trunk length, vce(robust)
      
          eststo: quietly: reg `2' price trunk length
          eststo: quietly: reg `2' price trunk length, vce(robust)
          
          esttab * using esttab-`=scalar(i)'.tex,
       
          scalar i = i+1
      }

      Comment

      Working...
      X