Announcement

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

  • How to call/use a macro defined outside of a program

    Hi there, I am wondering if there is a way to call/use a macro defined outside of a program in a program. For example, if I run the following codes, it pops out an error message -r(1)-


    Code:
    use https://stats.idre.ucla.edu/stat/data/hsb2, clear
    
        local ivlist "write"
        local dvlist "socst"
        local medlist "math"
      
        foreach iv of local ivlist{
            foreach med of local medlist{
                foreach dv of local dvlist{
                        preserve                
                            capture program drop ind
                            
                            program ind, rclass
                                gsem (`med' <- `iv') (`dv' <- `med' `iv')
                                return scalar ind = _b[`med':`iv']*_b[`dv':`med']
                            end
                            
                            bootstrap r(ind), reps(5) seed(666): ind
                            estat boot, bc
                        restore    
                }
            }
        }
    But if I run the following codes, it works perfectly. Thus, I believe it's a problem of how to call outside defined macro in a program. Can anyone give me some suggestions?

    Code:
    use https://stats.idre.ucla.edu/stat/data/hsb2, clear
    
    preserve
                            
        capture program drop ind
                                
        program ind, rclass
          
            local ivlist "write"
            local dvlist "socst"
            local medlist "math"
        
            gsem (`med' <- `iv') (`dv' <- `med' `iv')
            return scalar ind = _b[`med':`iv']*_b[`dv':`med']
    
        end
    
        bootstrap r(ind), reps(5000) seed(666): ind
        estat boot, bc
    
    restore
    Last edited by Victor Smith; 25 Sep 2018, 12:16.

  • #2
    It is part of the very essence of local macros that they cannot be used in this way. That is what makes them safe. It is also what makes them, in this instance, inconvenient. However, the workaround is easy: just pass the values of the local macros as arguments to your -program ind-.

    By the way, you should not have the definition of the program inside the loops. It should be defined only once and kept as is.

    Code:
    use https://stats.idre.ucla.edu/stat/data/hsb2, clear
    
        local ivlist "write"
        local dvlist "socst"
        local medlist "math"
      
    capture program drop ind
    
    program ind, rclass
        args iv dv med
        gsem (`med' <- `iv') (`dv' <- `med' `iv')
        return scalar ind = _b[`med':`iv']*_b[`dv':`med']
    end
    
      
        foreach iv of local ivlist{
            foreach med of local medlist{
                foreach dv of local dvlist{
                        preserve                
                            
                            bootstrap r(ind), reps(5) seed(666): ind `iv' `dv' `med'
                            estat boot, bc
                        restore    
                }
            }
        }
    Also, it isn't a good idea to reset the seed to 666 each time through the loop. You really don't want to build that kind of dependence into the bootstrap samples across the different analyses unless you have a clear and compelling reason to do so. So if I were you, I would -set seed 666- before you get to the loops, and omit the -seed()- option from -bootstrap-.

    Comment

    Working...
    X