Announcement

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

  • How to loop through multiple locals instead of all variables inside each local?

    Code:
    local ipv "angry fear intimacy_idx cont12_idx ev12_idx pv12_idx sv12_idx ipv_ovall_idx"
    local ipv_12_entire "cont12_idx ev12_idx pv12_idx sv12_idx cont_idx ev_idx pv_idx sv_idx"
    
    * IPV outcomes
    eststo clear
    foreach outcome of local ipv {
    eststo `outcome'_demo: reg `outcome' arm_free `demo_controls', vce(cluster new_csps)
    }
    
    estout * using "${Oleaf}/REG_IPV/panelA_ipv.tex", replace
    
    
    * Violence indexes (12 months VS entire marriage)
    eststo clear
    foreach outcome of local ipv_12_entire {
    eststo `outcome'_demo: reg `outcome' arm_free `demo_controls', vce(cluster new_csps)
    }
    
    estout * using "${Oleaf}/REG_IPV/panelA_ipv_12_entire.tex", replace

    QUESTION:
    If I do the above by running two separate regressions, it works as intended.
    However, I would like to run the two locals "ipv" and "ipv_12_entire" inside a loop.
    I did the following.
    Instead of looping over two locals as I'd like, the code below is looping every variable inside each local.
    Basically, the code below produces 16 .tex files (8 variables in local "ipv" and 8 variables in local "ipv_12_entire").
    Is there a way to write a loop with grouping locals? I just want to produce 2 .tex files.


    Code:
    loc i = 1
    
    eststo clear
    foreach outcome of local `ipv' `ipv_12_entire' {
    
    eststo `outcome': reg `outcome' arm_free `demo_controls', vce(cluster new_csps)
    
    estout * using "${Oleaf}/REG_IPV/panelA_`outcome'.tex", replace
    
    loc i = `i' + 1
    }​​​​​​​
    ​​​​​​​
    Last edited by Ei Thandar Myint; 31 May 2022, 22:37.

  • #2
    maybe place estout after the loop,
    Code:
    foreach outcome of local `ipv' `ipv_12_entire' {
    eststo `outcome': reg `outcome' arm_free `demo_controls', vce(cluster new_csps)
    }
    estout `ipv' using "${Oleaf}/REG_IPV/panelA_ipv.tex", replace
    estout `ipv_12_entire' using "${Oleaf}/REG_IPV/panelA_ipv_12_entire.tex", replace

    Comment


    • #3
      Perhaps the following examples will start you in a useful direction.
      Code:
      local ipv "angry fear intimacy_idx cont12_idx ev12_idx pv12_idx sv12_idx ipv_ovall_idx"
      local ipv_12_entire "cont12_idx ev12_idx pv12_idx sv12_idx cont_idx ev_idx pv_idx sv_idx"
      
      // two loops
      
      foreach outcome of local ipv {
          display "`outcome'"
      }
      
      foreach outcome of local ipv_12_entire {
          display "`outcome'"
      }
      
      // nested loops
      
      foreach list in ipv ipv_12_entire {
          foreach outcome of local `list' {
              display "`outcome'"
          }
      }
      
      // one loop
      foreach outcome in `ipv' `ipv_12_entire' {
          display "`outcome'"
      }
      Code:
      . local ipv "angry fear intimacy_idx cont12_idx ev12_idx pv12_idx sv12_idx ipv_ovall_idx"
      
      . local ipv_12_entire "cont12_idx ev12_idx pv12_idx sv12_idx cont_idx ev_idx pv_idx sv_idx"
      
      . 
      . // two loops
      . 
      . foreach outcome of local ipv {
        2.     display "`outcome'"
        3. }
      angry
      fear
      intimacy_idx
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      ipv_ovall_idx
      
      . 
      . foreach outcome of local ipv_12_entire {
        2.     display "`outcome'"
        3. }
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      cont_idx
      ev_idx
      pv_idx
      sv_idx
      
      . 
      . // nested loops
      . 
      . foreach list in ipv ipv_12_entire {
        2.     foreach outcome of local `list' {
        3.         display "`outcome'"
        4.     }
        5. }
      angry
      fear
      intimacy_idx
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      ipv_ovall_idx
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      cont_idx
      ev_idx
      pv_idx
      sv_idx
      
      . 
      . // one loop
      . foreach outcome in `ipv' `ipv_12_entire' {
        2.     display "`outcome'"
        3. }
      angry
      fear
      intimacy_idx
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      ipv_ovall_idx
      cont12_idx
      ev12_idx
      pv12_idx
      sv12_idx
      cont_idx
      ev_idx
      pv_idx
      sv_idx
      
      .

      Comment

      Working...
      X