Announcement

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

  • nested global macros within foreach loop - how do I return the global name? (not evaluate it)

    I am using nested global macros within a foreach loop
    I want to access the global name (to use it for labeling estimate outputs)

    Please help!

    I have a series of nested macros;
    Code:
    global GRP1 "1 2 3 4"
    global GRP2 "5 6 7"
    global GRP3 "8 9 10 11 12"
    
    global GRP12 "\$GRP1 \$GRP2"
    global GRP13 "\$GRP1 \$GRP3"
    ...
    global GRP123 "\$GRP1 \$GRP2 \$GRP3"
    which are then nested in a list of all possible combinations to be used in the foreach loop;
    Code:
    global ALL "\$GRP1 \$​GRP2 .... \$GRP12 ... \$GRP123"
    
    foreach X of global ALL {
    
    tokenize `X'
    local e_list ""
    
    while "`*'"!="" {
    local e "i.`1'"
    local e_list "`e_list' `e'"
    macro shift
    }
    
    nbreg DEP `e_list'
    this works fine but I want to access the active global name within each loop - to assign to estimates - something like

    Code:
    est save NBREG_`\X'
    where I would hope the estimate file name to be;

    NBREG_GRP1
    NBREG_GRP2
    ...
    NBREG_GRP123

    (i.e. the elements listed in $ALL)

    i have tried many combinations of `'; $; {} to no avail - and exhausted general searches for solutions.

    I'd be grateful for any ideas





  • #2
    For "global" read "local" passim, without a good reason. (Friendly nods towards Clyde Schechter.)

    That aside, why are you defining these macros with delayed evaluation? I see nothing untoward likely to happen with

    Code:
    global GRP1 1 2 3 4
    global GRP2 5 6 7
    global GRP3 8 9 10 11 12
    
    global GRP12 $GRP1 $GRP2
    global GRP13 $GRP1 $GRP3
    global GRP23 $GRP2 $GRP3 
    
    global GRP123 $GRP1 $GRP2 $GRP3
    where in passing I underline that the quotation marks are superfluous too.

    I would then rewrite your second block of code this way

    Code:
     
    foreach X in 1 2 12 13 23 123 {
        < reference to ${GRP`X'} > 
    }
    except that I can't follow what you are trying to do. Fit a series of models, I think, in which case defining your macros initially as bunches of variable names is likely to be a better idea.

    Although it's not much specific help, my general advice is that this code looks much more complicated than necessary.

    If need be, back up and tell us about the problem you are trying to solve, as the code seems to encapsulate several roundabout ways of thinking, and it's hard to see the flow beneath the roundabouts.



    Comment


    • #3
      Thanks for getting back to me Nick - I will search again looking at local references, perhaps I will have better luck

      I should have made it clearer that the macros are defining the bunches of variables - actually named EL1 EL2 EL3 etc - which fall into 4 groups
      hence they are defined initially to be evaluated later (apologies if I misunderstood your query on that)

      I need to run regression models for all possible combinations of these 4 groups (15 in total) - for a whole bunch of dependent variables (but not necessary to go into detail w.r.t. that 'outer loop')

      perhaps I complicated it (agree most likely unnecessarily) by calling on a loop of a global list... I will revise my code accordingly and investigate from there.

      many thanks

      your advice is very much appreciated (also don't know why I've used quotation marks around lists all these years so will eradicate that redundant bad habit - one less at least!)

      Comment


      • #4
        just to update, your suggestion to loop through a global list of numbers (instead of a global list of globals) has done the trick - thanks

        so my code now looks something like...

        Code:
        global ALL2 1 2 3 4 12 13 14 23 24 34 123 124 134 234 1234  
        
        foreach X of global ALL2 {  
        
        tokenize ${GRP`X'}
        local e_list ""
         
        while "`*'"!="" {
        local e "i.`1'"
        local e_list "`e_list' `e'"
        macro shift }  
        
        nbreg DEP `e_list'
        estimates save NBREG_GRP`X'  
        }
        will leave it there but i still can't help wondering why I was unable to loop through a list of globals and then return the name of the active global (without evaluating it)... perhaps it's not something that is ever necessary since the solution is a simple one.

        thanks again

        Comment


        • #5
          Thanks for closure. See also tuples (SSC).

          Comment

          Working...
          X