Announcement

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

  • Add variable names to a matrix when using mkmat

    Hi Statalist community,

    I am trying to use the mkmat command to create a matrix with the desired variables and then I want to export to an Excel spreadsheet. Below is sample code that illustrates my situation. I can't seem to add the variable names so that it appears in the excel spreadsheet. I am new to creating my own matrices and assistances would greatly be appreciated. Thanks in advance.

    Code:
    *pull in sample data
    use https://www.stata-press.com/data/r18/nhanes2l, clear
    *run regression
    logit highbp bmi age
    *estimate marginal effects by age
    margins, at(age = (20 (1) 60)) nose
    *put results in a matrix
    mat mat_results = nullmat(mat_results), r(b)'
    
    clear
    *Call up matrix
    svmat mat_results, names(MM)
    *Create an age variable
    gen age = _n + 17
    order age, first
    
    *Grab the age variable and marginal effects variable and put it in a matrix called matrix_group1
    *This is my attempt at adding variable names to the matrix. I am stuck here.
    mkmat age MM1, mat(matrix_group1) rownames(age)
    *Tell Stata which excel spreadsheet to use
    putexcel set practice.xlsx, modify
    *Tell Stata to put the matrix in cell A2
    putexcel A2= matrix(matrix_group1), rownames

  • #2
    Dear James,
    Besides reading the help file, it is usually also informative to read the more elaborate documentation in the PDF, which you (usually) can access by a left button mouse click on the link in the help file.
    So, that is what I did and therein it is explained under matrix options that:
    names
    also write row names and column names for matrix matname; may not be combined with rownames or colnames

    rownames
    also write matrix row names for matrix matname; may not be combined with names or colnames

    colnames
    also write matrix column names for matrix matname; may not be combined with names or rownames
    This implies by using rownames in your last line of code that you are excluding the variable names in the columns of your matrix, like:
    Code:
    putexcel A2= matrix(matrix_group1), rownames
    Therefore, replace rownames with names and you will get the desired result, like:
    Code:
    putexcel A2= matrix(matrix_group1), names
    http://publicationslist.org/eric.melse

    Comment


    • #3
      @ericmelse Thank you so much. This did the trick.

      Comment

      Working...
      X