Announcement

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

  • Foreach in mata

    Dear All,

    just a quick question. Is it possible to use foreach within a mata function?

    Suppose I have something lke:

    Code:
    mata:
    foreach v of global regressors{
        D = 1 :/ W_`v'
        W_`v'2 =  editmissing(1 :/ (D:^2), 0)
    }
    end
    I gen the following error message:

    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) -----------------------------------
    : foreach v of global regressors{
    'global' found where almost anything else expected
    (3 lines skipped)
    -------------------------------------------------------------------------------------------------------------
    Is there any mistake in my code or is it not possible to include foreach in mata? If not, is there a way to work this around?

    Thanks in advance.

  • #2
    foreach does not exist in Mata. You might get somewhere with code like this

    Code:
    local regressors foreign mpg
    mata
        regressors = st_local("regressors")
        regressors
        regressors = tokens(regressors)
        regressors
        for (i = 1 ; i <= cols(regressors) ; i++) {
            regressors[i]
        }
    end
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X