I have published a new package -- flexmat -- on the SSC. To install
Description
flexmat creates flexible matrices of text, numbers or both. This program is specifically useful when numeric and string results need to be stored in a matrix form or in a table. flexmat hardly throws any comfortability error. So we can append as well as insert cells, rows, columns, or even previously stored matrices to the current matrix without worrying about comfortability or about mixing reals with string. We can also delete rows or columns from a table / matrix. We can also merge two matrices. The merge is just like 1:1 merge of the official merge command.
Why flexmat?
The motivation for flexmat came from many requests related to making customizable tables with asdoc (One of my programs, available on the SSC). Though asdoc is extremely easy to use with many official commands of Stata such as sum, cor, reg, table, tab, ttest, etc. It is still challenging to create a custom table from bits and pieces of statistics coming from different lines of codes. To this end, asdoc offers the row() option, where tables are created row by row. The row() option is not flexible. You cannot create rows by cells. You cannot add columns. You cannot go back and drop rows or columns. You cannot replace cells or rows. All these limitations are taken care of in the flexmat.
An Example
Suppose we wish to create this table.
In the following example, I am using sub-commands reset, addcell, addrow, addcol, dropcol, and addmat. You can read about all sub-commands and options in the help file of flexmat or this page
Explanation:
addrow : sub-command to add a row of data to a table
data() : pass data contents, separated by a parse character
row(1) : specify row number; default is 1
col(1) : specify column number; default is 1
After the above line, flexmat table looks likes this. Note that numbers at the top of the table and at the left. These denote the row and column numbers for easy identification. These can be used later on while posting more data to the table.
The basic structure of the table is now ready. We can now add cells, rows, or columns to any row() and col() combination
Cells can be either replaced or added. Say we have the mean value of 5.1% and we want to post it to row(2) and col(2)
Assume the mean values are 6.74, 6.58, 3.7, 2.7 in years 2002, 2003, 2005, 2006. Therefore, use row(3) and col(2) as starting point for adding this data.
Both Stata and Mata matrices can be appended to the flexmat table. The mata matrix should be in string. Let us create a mata column vector and append it under the SD column
* Stata matrix can also be appended
* Drop elements
Both rows and columns can be dropped from the flexmat matrix. Let us drop column 5
How to export
Once the flexmat file is ready, it can be then exported to any of the asdocx's supported formats, that include MS Word, Excel, or LaTeX. asdocx is a premium version of asdoc and is available for a nominal fee of $9.99. Following is the asdocx syntax for exporting flexmat files.
Since the default output format is .docx, therefore, if just typed:
the flexmat file will be exported to a Word file with asdocx .docx format. In case we wish to send the output to an Excel file, then we would use the asdocx option save(Myfile.xlsx). For example:
We can also use other asdocx options such as font(font_name) to change the font family or fs(#) to set
the font size of the document.
Even if asdocx is not available, the flexmat table can be copy pasted into Excel.
Code:
ssc install flexmat
flexmat creates flexible matrices of text, numbers or both. This program is specifically useful when numeric and string results need to be stored in a matrix form or in a table. flexmat hardly throws any comfortability error. So we can append as well as insert cells, rows, columns, or even previously stored matrices to the current matrix without worrying about comfortability or about mixing reals with string. We can also delete rows or columns from a table / matrix. We can also merge two matrices. The merge is just like 1:1 merge of the official merge command.
Why flexmat?
The motivation for flexmat came from many requests related to making customizable tables with asdoc (One of my programs, available on the SSC). Though asdoc is extremely easy to use with many official commands of Stata such as sum, cor, reg, table, tab, ttest, etc. It is still challenging to create a custom table from bits and pieces of statistics coming from different lines of codes. To this end, asdoc offers the row() option, where tables are created row by row. The row() option is not flexible. You cannot create rows by cells. You cannot add columns. You cannot go back and drop rows or columns. You cannot replace cells or rows. All these limitations are taken care of in the flexmat.
An Example
Suppose we wish to create this table.
Code:
------------------------------------------------------------------------- 1 |Year Mean SD Min Max ----+--------------------------------------------------------------------- 2 |2001 5.1% 1.5% 0.00 10% 3 |2002 6.74 1.68 .012 11 4 |2003 6.58 1.61 0.025 9 5 |2005 0.370 0.680 0.1370 0.370 6 |2006 0.270 0.870 0.070 0.370 --------------------------------------------------------------------------
Code:
* Start with a clean slate flexmat reset * Create the title row flexmat addrow, data(Year, Mean, SD, Min, Max) row(1) col(1)
addrow : sub-command to add a row of data to a table
data() : pass data contents, separated by a parse character
row(1) : specify row number; default is 1
col(1) : specify column number; default is 1
After the above line, flexmat table looks likes this. Note that numbers at the top of the table and at the left. These denote the row and column numbers for easy identification. These can be used later on while posting more data to the table.
Code:
1 2 3 4 5 +------------------------------------+ 1 | Year Mean SD Min Max | +------------------------------------+
Code:
* Create the first column flexmat addcol, data(2001, 2002, 2003, 2005, 2006) row(2) col(1) 0 |1 2 3 4 5 ----+----------------------------------------------------------------------------- 1 |Year Mean SD Min Max ----+----------------------------------------------------------------------------- 2 |2001 3 |2002 4 |2003 5 |2005 6 |2006 ----------------------------------------------------------------------------------
Cells can be either replaced or added. Say we have the mean value of 5.1% and we want to post it to row(2) and col(2)
Code:
* Adding cells flexmat addcell, data(5.1% ) row(2) col(2)
Assume the mean values are 6.74, 6.58, 3.7, 2.7 in years 2002, 2003, 2005, 2006. Therefore, use row(3) and col(2) as starting point for adding this data.
Code:
* Add column flexmat addcol, data(6.74, 6.58, 3.7, 2.7 ) row(3) col(2) 0 |1 2 3 4 5 ----+----------------------------------------------------------------------------- 1 |Year Mean SD Min Max ----+----------------------------------------------------------------------------- 2 |2001 5.1% 3 |2002 6.74 4 |2003 6.58 5 |2005 3.7 6 |2006 2.7 ----------------------------------------------------------------------------------
Both Stata and Mata matrices can be appended to the flexmat table. The mata matrix should be in string. Let us create a mata column vector and append it under the SD column
Code:
* Add matrix mata: M = "2.1%"\ "2"\ "3.32" \ "6.3" \ "1.75" flexmat addmat, matname(M) row(2) col(3) 0 |1 2 3 4 5 ----+----------------------------------------------------------------------------- 1 |Year Mean SD Min Max ----+----------------------------------------------------------------------------- 2 |2001 5.1% 2.1% 3 |2002 6.74 2 4 |2003 6.58 3.32 5 |2005 3.7 6.3 6 |2006 2.7 1.75 ----------------------------------------------------------------------------------
Code:
mat S = 0\1\1.5\2.3\1 flexmat addmat, matname(S) row(2) col(4) nonames
Both rows and columns can be dropped from the flexmat matrix. Let us drop column 5
Code:
flexmat dropcol, col(5) 0 |1 2 3 4 ----+----------------------------------------------------------------------------- 1 |Year Mean SD Min ----+----------------------------------------------------------------------------- 2 |2001 5.1% 2.1% 0 3 |2002 6.74 2 1 4 |2003 6.58 3.32 1.5 5 |2005 3.7 6.3 2.3 6 |2006 2.7 1.75 1 ----------------------------------------------------------------------------------
Once the flexmat file is ready, it can be then exported to any of the asdocx's supported formats, that include MS Word, Excel, or LaTeX. asdocx is a premium version of asdoc and is available for a nominal fee of $9.99. Following is the asdocx syntax for exporting flexmat files.
Code:
asdocx exportflex , [asdocx_options]
Code:
asdocx exportflex
Code:
asdocx exportflex, save(Myfile.xlsx)
the font size of the document.
Even if asdocx is not available, the flexmat table can be copy pasted into Excel.
Comment