*! version 0.31

Title

mat2xl -- convert a matrix expression into a string formatted Mata matrix
and inserts the string matrix into a sheet in an Excelbook

Syntax

mat2xl
matrixexpression
[,
options]

Main
toxl(string) A string containing up to 5 values separated
by a comma. The values are:

* path and filename on the excel book to save in. Excel book suffix is set/reset to xls for Stata 13 and to xlsx for Stata 14 and above

* the sheet name to save output in

* (Optional) replace - replace/overwrite the content in the sheet

* (Optional) row, column numbers for the upper right corner of the table in the sheet

* (Optional) columnn widths in parentheses. If more columns than widths the last column width is used for the rest

Optional
rownamewidth(#) Set width for row equation and row name columns.
Default value is 25

cellwidth(#) Set width for cell columns.
Default value is 15

decimals(string) Matrix of integers specifying numbers of
decimals at cell level.
If the matrix is smaller than the data matrix the right most column is copied
to get the same number of columns.
And likewise for the rows

hidesmall(#) If set hide all values below the set value.
Default is the value 0, ie no hidding

noisily For debugging. Show selected values

noCleanmata For debugging. Do not delete Mata values and instances

Description

mat2xl convert a matrix expression into a string formatted Mata matrix
and inserts the string matrix into a sheet in an Excelbook.
Works for Stata version 13 and up.

Examples

Generate example matrix by sumat:

`"sysuse auto, clear"'

`"sumat price , stat(mean ci) rowby(rep78) colby(foreign)"'

Insert the returned matrix r(sumat) starting in
cell (A1, row = 1 column = 1) at the "tbl1" sheet in Excel book "deleteme.xls(x)".

No matter what suffix that is suggested in the command, the suffix naming
convention is xls for Stata version 13 and xlsx for Stata version 14 and up.

`"mat2xl r(sumat), toxl(deleteme, tbl1)"'

Insert the returned matrix r(sumat) starting in
cell (A20, row = 20 column = 1) at the "tbl1" sheet in Excel book "deleteme.xls(x)".

`"mat2xl r(sumat), toxl(deleteme, tbl1, 20, 1)"'

Insert the returned matrix r(sumat) starting in
cell (C2, row = 2 column = 3) at the "tbl2" sheet in Excel book "deleteme.xls(x)".

The sheet is replaced so old values are deleted.

First column width is set to 30. The rest is set to 15.

Values are shown with 3 decimals

`"mat2xl r(sumat), toxl(deleteme.xlsx, tbl2, replace, 2,3 , (30,15)) decimals(3)"'

If MS Excel is installed see the result:

Stata 13: `"shell "deleteme.xls""'

Stata 14 and up: `"shell "deleteme.xlsx""'

Author and support

Author:

Niels Henrik Bruun,

Aalborg University Hospital

Support:

niels.henrik.bruun@gmail.com