Announcement

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

  • Global of globals in loops

    Hi everyone!

    I’m trying to use a global of globals in a loop. Let me clarify: I have three global macros - "a", "b", and "c" - each containing a list of variables, for example: global a "1 2 3", global b "4 5 6 7", and global c "8 9". I need to run some regressions for each group of variables, so I’d like to create a new global that contains the names of these three original globals, like global outcomes "a b c". This would allow me to loop through each regression for every group of variables.


    Here is the working code I currently have:

    global a "1 2 3"
    global b "4 5 6 7"
    global c "8 9"

    /*Tab 1: a */
    eststo clear
    foreach var in $a {
    mainreg `var'
    }
    esttab using "$tex_maintables\rc1\a", $options


    /*Tab 2: b */
    eststo clear
    foreach var in $b{
    mainreg `var'
    }
    esttab using "$tex_maintables\rc1\b", $options


    /* Tab 3: c*/
    eststo clear
    foreach var in $c{
    mainreg `var'
    }
    esttab using "$tex_maintables\rc1\c", $options



    And here is how I attempted to shorten it using a loop, but it does not work properly:

    global a "1 2 3"
    global b "4 5 6 7"
    global c "8 9"
    global outcome "a b c"

    eststo clear
    foreach group in $outcome {

    foreach var in $`group' {

    mainreg `var'
    esttab using "$tex_maintables\rc1\$outcome", $options
    }
    }


    My issue is how to handle globals within a global.

    I hope my question is clear, but if you need further clarification, please ask. Thank you for your time!

  • #2
    Your solution seems to work:

    Code:
    . global a "1 2 3"
    
    . global b "4 5 6 7"
    
    . global c "8 9"
    
    . global outcome "a b c"
    
    .
    . eststo clear
    
    . foreach group in $outcome {
      2.         foreach var in $`group' {
      3.                 di `var'
      4.         }
      5. }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Maybe the problem is with the mainreg command, since I don't know what that is.

    More generally, I replace globals with locals in that application. They are much saver in the sense that locals guarantee that all output comes from a .do file and that .do file alone. That is important to ensure that your research is reproduceable.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X