Announcement

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

  • Using locals to combine matrices

    Hi all,

    I have a set of 50 matrices derived from outputs run for 50 variables with the same names. I am trying to combine these matrices to put them in an excel sheet. As an example, this is how I have combined 4 of them:

    Code:
    matrix comb = (h_region \ h_wealth \ h_urban \ m_edu)
    I have used different versions of this combined matrix and the variable names throughout my code, so rather than retyping the names of all the variables and matrices each time, I would like to include them in a local macro, like this:

    Code:
    local x_all = "h_region h_wealth h_urban m_edu"
    
    foreach x in `x_all' {
        local `x'n = "`x' \ "
        di "``x'n'"
        }
    di "``x'n'"
    
    matrix x_all = ("``x'n'")
    matrix list x_all
    
    matrix comb = (h_region \ h_wealth \ h_urban \ m_edu)
    matrix list comb
    I can get the local macro to add a \ after each matrix name, but I'm not sure how to get it to display all of the matrix names with the separator between them in a new local macro. This might also not be the best way of doing this, so any suggestions appreciated!

    Thanks,

    Sonia



  • #2
    Your line using backslashes does the job. Otherwise you are putting names in macros and then taking them out again. The macro part could be skipped in this case.

    However, if you wanted to automate the job of separating names by operators you could go

    Code:
    local x_all h_region h_wealth h_urban m_edu
    local expression : subinstr local x_all " " " \ ", all 
    di "`expression'"
    That hinges on names being initially separated by single spaces, but lists emitted by Stata are typically of that kind and there is functionality to clean up multiple spaces if you need it.

    Comment


    • #3
      Hi Nick,

      Thanks, this is really helpful! I'm also wondering if there is a way of giving the matrix names different tags or suffixes when they are produced from different statistical commands in the code, without putting names into macros and taking them out again. So for example, it would be:

      Code:
      matrix comb_a = (h_region_a \ h_wealth_a \ h_urban_a \ m_edu_a)
      matrix comb_b = (h_region_b \ h_wealth_b \ h_urban_b \ m_edu_b)
      Sonia

      Comment


      • #4
        You could write a loop

        Code:
        foreach g in a b { 
              matrix comb_`g' = (h_region_`g' \ h_wealth_`g' \ h_urban_`g' \ m_edu_`g')
        }
        If you had several of these, that could be worthwhile.

        Comment


        • #5
          Hi Nick,

          Thanks again. I managed to solve my problem by using your local expression suggestion, combined with something you had suggested to another user in this post https://www.stata.com/statalist/arch.../msg00344.html :

          Code:
          local x_all h_region h_wealth h_urban m_edu
          local x_all_b
          foreach b of local x_all {
                       local x_all_b `x_all_b' `b'_b
                       }
          di "`x_all_b'"
          local expression : subinstr local x_all_b " " " \ ", all 
          di "`expression'"
          Sonia

          Comment

          Working...
          X