Announcement

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

  • sureg error when using global equation list

    Hello,

    I am attempting to use a dynamic list of globals as an input into sureg. In particular, in the program setup I am requiring a user to declare both the number of equations and globals representing the equations:

    Code:
    set more off
    
    //Load data here
    sysuse auto, clear
    *Specify model equations *Set the number of equations after the equal sign scalar numEquations = 3 *Each equation should have a global declaration of the form : *global eq1 (Y X1 X2 ...Xk) *Example global eq1 (price foreign weight length) global eq1 (price foreign weight length) global eq2 (mpg foreign weight) global eq3 (displ foreign weight)
    I later use a forvalues loop to build the input list for sureg:
    Code:
    *List which equations to include in the regression
    forvalues i=1(1)`=numEquations' {
      local tmp "eq`i'"
    local eqList `eqList' char(36)+`tmp'+char(32)
    }
    di "`eqList'"


    However, when I call sureg:

    Code:
    sureg `eqList', const(`constList')
    I am getting an error, "coding operators not allowed". Any assistance on this issue would be much appreciated.

    Thanks,
    Erica

    The full code is below:

    Code:
     set more off
    
    //Load data here
    sysuse auto, clear
    
    ////////////////////////////////////////////////////////////
    /*Step One: Regression setup */
    //////////////////////////////////////////////////////////
    *Specify model equations
    *Set the number of equations after the equal sign
    scalar numEquations = 3
    
    *Each equation should have a global declaration of the form :
    *global eq1 (Y X1 X2 ...Xk)
    *Example global eq1 (price foreign weight length)
    global eq1 (price foreign weight length)
    global eq2 (mpg foreign weight)
    global eq3 (displ foreign weight)
    
    ////////////////////////////////////////////////////////////
    /*Step Two: Run Regression */
    //////////////////////////////////////////////////////////
    *List which equations to include in the regression
    forvalues i=1(1)`=numEquations' {
      local tmp "eq`i'"
      local eqList `eqList' char(36)+`tmp'+char(32)
      }
    di "`eqList'"
    
    sureg `eqList'
    Last edited by Erica Clower; 18 Jan 2017, 16:47.

  • #2
    Well, I think there is a way to fix this, but since you shouldn't do this anyway, I'll show you a better way. Global macros are an inherently unsafe programming practice and should only be used as a last resort when no other way of passing information is available. Global macros are unsafe because they are subject to name clashes with global macros defined in other programs that are running with yours. And since you do not necessarily know, nay, cannot necessarily know in advance which other programs my be running with yours, you are setting yourself up for terrible bugs that are excruciatingly difficult to find and fix. So the safe way to do this is:

    Code:
    set more off
    
    //Load data here
    sysuse auto, clear
    
    ////////////////////////////////////////////////////////////
    /*Step One: Regression setup */
    //////////////////////////////////////////////////////////
    *Specify model equations
    *Set the number of equations after the equal sign
    local numEquations 3
    
    local eq1 (price foreign weight length)
    local eq2 (mpg foreign weight)
    local eq3 (displ foreign weight)
    
    ////////////////////////////////////////////////////////////
    /*Step Two: Run Regression */
    //////////////////////////////////////////////////////////
    *List which equations to include in the regression
    local eqList
    forvalues i=1(1)`numEquations' {
        local eqList `eqList'`eq`i''
    }
    di "`eqList'"
    
    sureg `eqList'
    Added: This approach also has the advantage that the code is simpler and more transparent.

    Comment


    • #3
      Your suggestion is indeed much simpler and more transparent (plus it runs). It seems that I have fallen into the bad habit of using global macros. I will take your warnings to heart moving forward.

      Much thanks,
      Erica

      Comment

      Working...
      X