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

  • Moving macro lists into Mata vectors

    Supposing I have a local or global macro with a set of numeric or string values, so a list, and I wish to use them in Mata as a vector. Is there a simple way to do that, perhaps with a Mata analogue of -word count- which would allow me to formulate a -for-/-foreach- loop?

    To make this concrete, here's a example.

    local have_list `" "This is" "my list" "to" "test" "'
    mata want = J(1, 0, "")
    foreach item of local have_list {
      mac list _item
      mata: item = st_local("item")
      mata: want = want , item
    mata: want
    This bit of code does what I want it to do, and -want- is correct. Is there a better or more direct approach to this task? I have considered -ustrsplit()- but this can't work in all situations, such as in the above when a space can both delimit the list and be present within each item.

  • #2
    Perhaps this will point in a useful direction.
    . local have_list `" "This is" "my list" "to" "test" "'
    . macro list _have_list
    _have_list:      "This is" "my list" "to" "test"
    . local com_list : subinstr local have_list `"" ""' `"",""' , all
    . macro list _com_list
    _com_list:       "This is","my list","to","test"
    . mata want = (`com_list')
    . mata want
                 1         2         3         4
      1 |  This is   my list        to      test  |


    • #3
      Thank you, William. That's a nice way to tackle the problem.