Announcement

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

  • Create a List from a Vector

    Is there an efficient way of moving a column or row vector with strings into a list so as to use with macrolists in Stata? Since the vectors can be thousands of values, I would rather not loop over each element.

    I have a two string matrices (vectors, actually) in Mata containing values of a numeric variable, including extended missing values stored as .a .b. etc. I can move these to Stata matrices.

    I ultimately would like a macro list with the elements of A that are not in B. Were they in list form, I could easily get this with : list A - B.

    (A couple of almost solutions in Mata: this post gives a way to get the common elements from real matrices; and -indexnot- only works 1 character elements.) I'm a Mata newbie, so a Stata solution would be easier for me to understand, but I'm open to Mata if there is an efficient solution.

    Code:
    mata: A=(1, 2, 3, 10, .a, .b, .c)
    mata: B=(1, 2, .a)
    mata: st_matrix("A", A)
    mata: st_matrix("B", B)
    mat list A
    mat list B

    Thanks,
    Carole
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

  • #2
    If there are no embedded spaces in your Mata vector, you can get it directly into a Stata local. Using your example

    Code:
    mata: A=(1, 2, 3, 10, .a, .b, .c)
    mata: B=(1, 2, .a)
    
    mata : st_local("A", invtokens(strofreal(A)))
    mata : st_local("B", invtokens(strofreal(B)))
    
    local A_not_in_B : list A - B
    display "`A_not_in_B'"
    Best
    Daniel

    Comment


    • #3
      I don't know about efficient: in terms of speed; storage; something else? Does efficient mean elegant?

      Any way, here is one way to do it. It could no doubt be made shorter at the cost of a little clarity. I haven't tried much bigger examples.

      Code:
       mata
       A=(1, 2, 3, 10, .a, .b, .c)
       B=(1, 2, .a)
       st_local("Alist", invtokens(strofreal(A)))
       st_local("Blist", invtokens(strofreal(B)))
       end
       local diff : list Alist - Blist
       mac li

      Comment


      • #4
        Thank you Daniel & Nick!
        Any suggestions on a good Mata book? I love the Stata manuals--they are extremely clear, but I have a very difficult time understanding the Mata manual. The start of each entry is a complete mystery to me. I re-read the beginning hoping that I overlooked something, but I need a primer on how to read the manual!
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          There is no book dedicated to Mata. Many people wish that weren't so.

          Comment


          • #6
            Some good resources are mentioned here.

            Best
            Daniel

            Comment


            • #7
              Much appreciated, Daniel!
              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment

              Working...
              X