Announcement

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

  • Correct use of constraint in foreach cycle

    Hi, I would like to set constraints later use them in a program. Here is a small example:

    sysuse auto, clear

    regress price weight mpg

    local indep "weight mpg"

    local ind=1
    foreach var of varlist `indep' {
    constraint `ind' `var'=_b[`var']
    local ++ind
    }

    What I get is

    . constraint list
    1: weight=_b[weight]
    2: mpg=_b[mpg]


    So, my question is why my constraints do not have numbers, representing regression coefficients on the right hand sides?

    I also tried modification:

    sysuse auto, clear

    regress price weight mpg

    local indep "weight mpg"

    local ind=1
    foreach var of varlist `indep' {
    local b`var'=_b[`var']
    constraint `ind' `var'=`bvar'
    local ++ind
    }


    but then I get

    . constraint list
    1: weight=
    2: mpg=

    Again no numbers, just blanks.

  • #2
    You need to use the inline expansion operator `= ' around the _b[var] term.
    Code:
    sysuse auto,clear
    local i = 1
    reg price weight mpg
    foreach var of varlist weight mpg {
        constraint `i' `var' = `=_b[`var']'
        local ++i
    }
    constraint list

    Comment


    • #3
      Thanks Scott! It work nicely.

      Comment

      Working...
      X