Announcement

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

  • Turning dataset into matrix by using mkmat

    Dear all,
    Hope all is well.

    I am trying to turn values in the data set into matrix by using mkmat command.

    However, as there are too many values in the varlist, it keeps failing to form a matrix.

    Is there any way that I can drop some values under the varlist and form a matrix?

    For example, Is there a way that I form a matrix with the top 10 values under a varlist?


    Thanking you in advance.

  • #2
    Nothing stops you from using the commands preserve and restore to first restrict the number of observations, form the matrix and then revert to the original dataset.

    For example, Is there a way that I form a matrix with the top 10 values under a varlist?
    Do you mean top 10 observations or you are thinking of sorting the observations and picking the highest or lowest first? Remember that if you need to sort, you can sort in terms of only one variable in the varlist.


    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . preserve
    
    . keep  price mpg weight length displacement
    
    
    *SORT IN DESCENDING ORDER IN TERMS OF PRICE (HIGHEST TO LOWEST)
    . gsort  -price mpg weight length displacement
    
    
    . keep in 1/10
    (64 observations deleted)
    
    . mkmat price mpg weight length displacement, mat(auto)
    
    . restore
    
    . mat list auto
    
    auto[10,5]
                price           mpg        weight        length  displacement
     r1         15906            21          4290           204           350
     r2         14500            14          3900           204           350
     r3         13594            12          4720           230           400
     r4         13466            14          3830           201           302
     r5         12990            14          3420           192           163
     r6         11995            17          3170           193           163
     r7         11497            12          4840           233           400
     r8         11385            14          4330           221           425
     r9         10372            16          3880           207           231
    r10         10371            16          4030           206           350



    If you are using an older version of Stata (I believe versions prior to Stata 12), you can increase the memory before starting your session if this is a space issue. See

    Code:
    help memory


    Comment


    • #3
      Please refer to the below code or command for my question.
      From the code, you will get a (2X10) matrix named c.
      'in 1/10' indicates that you will form a matrix with values from the first row to 10th row under the varlist 1 and 2.

      [code]:
      mkmat varlist1 varlist2 in 1/10, matrix(c)



      Oh! I did not know there was a reply to my question.
      Thank you Mr.Musau for your reply! your code seems to work pretty good for me.

      fyi, by top 10 observations, I just meant the observation from 1st row to 10th row.
      Last edited by Sungmin Cheu; 18 May 2016, 06:51.

      Comment


      • #4
        Seems to be resolved now by your edit!
        Last edited by Andrew Musau; 18 May 2016, 07:12.

        Comment


        • #5
          mkmat with the in qualifier is the shortest way, but here is another way

          Code:
          sysuse auto ,clear
          
          local var price mpg weight length displacement
          mata: st_matrix("c",st_data((1,10),"`var'"))
          mat coln c = `var'
          mat li c

          Comment

          Working...
          X