|  | 
|  | 1 | +* Feather: output matrix to Stata interface, Word and LaTeX | 
|  | 2 | +* Author: Meiting Wang, Master, School of Economics, South-Central University for Nationalities | 
|  | 3 | +* Email: 2017110097@mail.scuec.edu.cn | 
|  | 4 | +* Created on Oct 26th, 2019 | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +program define wmtmat | 
|  | 8 | +version 15.1 | 
|  | 9 | + | 
|  | 10 | +syntax name(id="a matrix name") [using/] [, /// | 
|  | 11 | +	replace append FMT(string) ROWSFMT(string) COLSFMT(string) /// | 
|  | 12 | +	TItle(string) Alignment(string) PAGE(string)] | 
|  | 13 | +/* | 
|  | 14 | +optional illustration: | 
|  | 15 | +1. name: only a matrix name permitted. | 
|  | 16 | +2. fmt(): set the overall format for the reported matrix. | 
|  | 17 | +3. rowsfmt(): set the format of each row of the matrix separately. | 
|  | 18 | +4. colsfmt(): set the format of each column of the matrix separately. | 
|  | 19 | +5. title(): set the title for the reported table, matrix name as the default. | 
|  | 20 | +6. alignment(): only used in the LaTeX output, set the column format of the LaTeX | 
|  | 21 | +table, but it will not impact the column format in the Stata output table, dot as  | 
|  | 22 | +the default. | 
|  | 23 | +7. page(): only used in the LaTeX output,set the extra package for the LaTeX code. | 
|  | 24 | +please don't need to add the package of booktabs array dcolumn, because the code  | 
|  | 25 | +will automatic process these with the option of alignment(). | 
|  | 26 | +*/ | 
|  | 27 | + | 
|  | 28 | + | 
|  | 29 | +*--------设置默认格式------------ | 
|  | 30 | +local default_fmt "%11.3f" | 
|  | 31 | +local default_la_fmt "%11.3fc" | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +*---------程序不合规时的报错----------- | 
|  | 35 | +if ("`replace'`append'"!="")&("`using'"=="") { | 
|  | 36 | +	dis "{error:replace or append can't appear when you don't need to output result to a file.}" | 
|  | 37 | +	exit | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +if ("`replace'"!="")&("`append'"!="") { | 
|  | 41 | +	dis "{error:replace and append cannot appear at the same time.}" | 
|  | 42 | +	exit | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +if ("`fmt'"!="")&("`rowsfmt'"!="") { | 
|  | 46 | +	dis "{error:fmt and rowsfmt can't appear at the same time.}" | 
|  | 47 | +	exit | 
|  | 48 | +} | 
|  | 49 | +if ("`fmt'"!="")&("`colsfmt'"!="") { | 
|  | 50 | +	dis "{error:fmt and colsfmt can't appear at the same time.}" | 
|  | 51 | +	exit | 
|  | 52 | +} | 
|  | 53 | +if ("`rowsfmt'"!="")&("`colsfmt'"!="") { | 
|  | 54 | +	dis "{error:rowsfmt and colsfmt can't appear at the same time.}" | 
|  | 55 | +	exit | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +if "`fmt'" != "" { | 
|  | 59 | +	local fmt_num: word count `fmt' | 
|  | 60 | +	if `fmt_num' != 1 { | 
|  | 61 | +		dis "{error:fmt needs to be a single word.}" | 
|  | 62 | +		exit | 
|  | 63 | +	} | 
|  | 64 | +} | 
|  | 65 | +if "`rowsfmt'" != "" { | 
|  | 66 | +	local rowsfmt_num: word count `rowsfmt' | 
|  | 67 | +	if `rowsfmt_num' != rowsof(`namelist') { | 
|  | 68 | +		dis "{error:the number of words in rowsfmt() can't match the number of rows in the matrix `namelist'.}" | 
|  | 69 | +		exit | 
|  | 70 | +	} | 
|  | 71 | +} | 
|  | 72 | +if "`colsfmt'" != "" { | 
|  | 73 | +	local colsfmt_num: word count `colsfmt' | 
|  | 74 | +	if `colsfmt_num' != colsof(`namelist') { | 
|  | 75 | +		dis "{error:the number of words in colsfmt() can't match the number of cols in the matrix `namelist'.}" | 
|  | 76 | +		exit | 
|  | 77 | +	} | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | +if (~ustrregexm("`using'",".tex"))&("`alignment'`page'"!="") {  | 
|  | 81 | +	dis "{error:alignment and page can only be used in the LaTeX output.}" | 
|  | 82 | +	exit | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | + | 
|  | 86 | +*---------前期语句处理---------- | 
|  | 87 | +*普通选项语句的处理 | 
|  | 88 | +if "`using'" != "" { | 
|  | 89 | +	local us_ing "using `using'" | 
|  | 90 | +} | 
|  | 91 | +if "`title'" == "" { | 
|  | 92 | +	local title "Matrix `namelist'[`=rowsof(`namelist')',`=colsof(`namelist')']" | 
|  | 93 | +} | 
|  | 94 | + | 
|  | 95 | +*fmt系列语句构建 | 
|  | 96 | +if "`fmt'" != "" { | 
|  | 97 | +	local st_fmt "fmt(`fmt')" | 
|  | 98 | +	local st_fmt_la "fmt(`fmt')" | 
|  | 99 | +} | 
|  | 100 | +else if "`rowsfmt'" != "" { | 
|  | 101 | +	local st_fmt `"fmt("`rowsfmt'")"' | 
|  | 102 | +	local st_fmt_la `"fmt("`rowsfmt'")"' | 
|  | 103 | +} | 
|  | 104 | +else if "`colsfmt'" != "" { | 
|  | 105 | +	local st_fmt "fmt(`colsfmt')" | 
|  | 106 | +	local st_fmt_la "fmt(`colsfmt')" | 
|  | 107 | +} | 
|  | 108 | +else { | 
|  | 109 | +	local st_fmt "fmt(`default_fmt')" | 
|  | 110 | +	local st_fmt_la "fmt(`default_la_fmt')" | 
|  | 111 | +} //默认值 | 
|  | 112 | + | 
|  | 113 | +*构建esttab中alignment()和page()内部的语句(LaTeX输出专属) | 
|  | 114 | +if "`page'" != "" { | 
|  | 115 | +	local page ",`page'" | 
|  | 116 | +} | 
|  | 117 | +if "`alignment'" == "math" { | 
|  | 118 | +	local page "array`page'" | 
|  | 119 | +	local alignment "*{`=colsof(`namelist')'}{>{$}c<{$}}" | 
|  | 120 | +	 | 
|  | 121 | +} | 
|  | 122 | +else { | 
|  | 123 | +	local page "array,dcolumn`page'" | 
|  | 124 | +	local alignment "*{`=colsof(`namelist')'}{D{.}{.}{-1}}" | 
|  | 125 | +} | 
|  | 126 | +//加上array宏包可使得表格线之间的衔接没有空缺 | 
|  | 127 | + | 
|  | 128 | + | 
|  | 129 | +*-----------------主程序--------------- | 
|  | 130 | +esttab matrix(`namelist', `st_fmt'), compress /// | 
|  | 131 | +	nomtitles title(`title')  //Stata 界面显示 | 
|  | 132 | +if ustrregexm("`us_ing'",".rtf") { | 
|  | 133 | +	esttab matrix(`namelist', `st_fmt') `us_ing', compress `replace'`append' /// | 
|  | 134 | +		nomtitles title(`title')  //Word 显示 | 
|  | 135 | +} | 
|  | 136 | +if ustrregexm("`us_ing'",".tex") { | 
|  | 137 | +	local col_names: colnames `namelist' | 
|  | 138 | +	tokenize "`col_names'" | 
|  | 139 | +	local i = 1 | 
|  | 140 | +	local col_new_names "" | 
|  | 141 | +	while "``i''" != "" { | 
|  | 142 | +		local col_new_names "`col_new_names'\multicolumn{1}{c}{``i''} " | 
|  | 143 | +		local `i' "" //置空`i' | 
|  | 144 | +		local i = `i' + 1 | 
|  | 145 | +	} | 
|  | 146 | +	mat colnames `namelist' = `col_new_names' | 
|  | 147 | +	esttab matrix(`namelist', `st_fmt_la') `us_ing', compress `replace'`append' /// | 
|  | 148 | +		nomtitles title(`title')  booktabs width(\hsize) page(`page') /// | 
|  | 149 | +		alignment(`alignment')  | 
|  | 150 | +	mat colnames `namelist' = `col_names' | 
|  | 151 | +} //LaTeX 显示 | 
|  | 152 | +end | 
0 commit comments