Announcement

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

  • Export Mata string matrix to Stata and Excel

    Hello
    I'm working with string and numeric vectors in mata, and I need to export them to Excel. For numeric vectors, I use the matrix conversion to Stata and then the putexcel command, but for the string case I don't know what to do, because Stata does not admit "string" vectors. So, any help will be welcome!
    Best




  • #2
    Can't you pass the string vector directly from Mata via its xl() class? In Stata's command line, type
    Code:
    help mf_xl
    for further information.

    Comment


    • #3
      You may use -getmata- to turn the Mata matrix into a Stata dataset, then use -export excel-, for example

      Code:
      . mata:
      ------------------------------------------------- mata (type end to exit) ------
      : 
      : A = J(3, 2, "test")
      
      : 
      : A
                1      2
          +---------------+
        1 |  test   test  |
        2 |  test   test  |
        3 |  test   test  |
          +---------------+
      
      : 
      : end
      --------------------------------------------------------------------------------
      
      . 
      . getmata (var*) = A
      
      . 
      . list
      
           +-------------+
           | var1   var2 |
           |-------------|
        1. | test   test |
        2. | test   test |
        3. | test   test |
           +-------------+
      
      . 
      . export excel using test.xlsx, replace
      file test.xlsx saved
      
      .

      Comment


      • #4
        I came up with a good solution to this problem. You can use the colnames/rownames function, which lets you give your Stata matrix column and row names (in string!). If your exist matrix has these already, simply create a blank matrix and use the function purely to get a bunch of text into a matrix.

        In my application I made a blank (empty) matrix of the correct dimensions, and then gave the headings I needed. For example, I wanted to place the text "count count mean mean median median" above 6 of my columns, and paste a different matrix (with its own headings) below.

        My code looked like this:

        Code:
        matrix colempty=J(1,6,.)
        matrix colnames colempty=count count mean mean median median
        putexcel a1=matrix(colempty), colnames

        Comment

        Working...
        X