Announcement

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

  • "foreach" questions?

    Dear All, I have 4 key explanatory variables (privo, lly, dby, btot) and 3 conditioning information sets (empty, policy, and full as defined below)
    Code:
    local empty ""
    local policy "school60 gov pi bmp trade"
    local full "school60 gov pi bmp trade revc assass avelf"
    
    // legal origins (and its interactions with gap60)
    local ivs "englishn frenchn germann englishn_gap frenchn_gap germann_gap"
    
    local all "growth privo gap60 privo_gap lly lly_gap dby dby_gap btot btot_gap `full'"
    local f "word excel dec(3) sortvar(`all')"
    
    /*
    ivreg2 growth (privo privo_gap = `ivs') gap60, robust
    outreg2 using "log\ahm05qje-t1", `f' ctitle(privo_empty) replace
    ivreg2 growth (privo privo_gap = `ivs') gap60 `policy', robust
    outreg2 using "log\ahm05qje-t1", `f' ctitle(privo_policy) append
    ivreg2 growth (privo privo_gap = `ivs') gap60 `full', robust
    outreg2 using "log\ahm05qje-t1", `f' ctitle(privo_full) append
    */
    
    local a replace
    foreach v of varlist privo lly dby btot {
      foreach ctr of varlist `empty' `policy' `full' {
        ivreg2 growth (`v' `v'_gap = `ivs') gap60 `ctr', robust
        outreg2 using "log\ahm05qje-t1", `f' ctitle(`v'_`cts') `a' 
        local a append  
      }
    }
    As such, I expect to have results on 4*3=12 regressions. However, the above procedure offer 52 regression outcomes. I am wondering if something wrong with the command
    Code:
    foreach ctr of varlist `empty' `policy' `full' {
    Any suggestions are highly appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Stata reads the line
    Code:
    foreach ctr of varlist `empty' `policy' `full' {
    as:
    Code:
    foreach ctr of varlist school60 gov pi bmp trade school60 gov pi bmp trade revc assass avelf {

    You might, instead, try:
    Code:
    foreach ctr in empty policy full {
    ivreg2 growth (`v' `v'_gap = `ivs') gap60 ``ctr'', robust
    Should the macro cts below be ctr?
    Code:
    utreg2 using "log\ahm05qje-t1", `f' ctitle(`v'_`cts') `a'
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      This will solve the problem.
      Code:
        foreach ctr in "`empty'" "`policy'" "`full'" {
      With the quotation marks, ctr becomes one of the three lists of variables. Without the quotation marks, ctr becomes each variable in each list in turn.
      Code:
      . local empty ""
      
      . local policy "school60 gov pi bmp trade"
      
      . local full "school60 gov pi bmp trade revc assass avelf"
      
      . 
      . foreach ctr in "`empty'" "`policy'" "`full'" {
        2. display "`ctr'"
        3. }
      
      school60 gov pi bmp trade
      school60 gov pi bmp trade revc assass avelf
      
      . 
      . foreach ctr in `empty' `policy' `full' {
        2. display "`ctr'"
        3. }
      school60
      gov
      pi
      bmp
      trade
      school60
      gov
      pi
      bmp
      trade
      revc
      assass
      avelf

      Comment


      • #4
        Dear Carole, Many thanks for your helpful solution, and sorry for the typo (cts should be ctr).
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Dear William, Thanks for the suggestion. It can run, but does not work for ctitle(`v'_`ctr') [sorry for the typo: it should be cts]. Sorry for not being addressing this point earlier.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X