Tables with frmttable

frmttable is a user-written command to create very flexibly nicely formatted tables as .rtf (which you can use in Word) or .tex files. The command can be downloaded by writing

net install sg97_5, from(http://www.stata-journal.com/software/sj12-4/)

As the putexcel command, it takes a matrix as input. In contrast to the putexcel command, you can combine matrices before you export them, and you can display the resulting table in Stata before exporting it.

frmttable and putexcel

You can take the example from the previous tab to create the same table as .rtf or .tex using frmttable.

sysuse census, clear

matrix d = J(6,5,.)					// creates matrix that will store results which will be exported
matrix colnames d = "N" "Mean" "SD" "Min" "Max"
			
local i = 1

foreach var of varlist pop* {

	local name: variable label `var'
	local namelist `"`namelist' "`name'""'		// store all names
	
	qui sum `var', detail
	mat d[`i',1] = r(N)
	mat d[`i',2] = r(mean)
	mat d[`i',3] = r(sd)
	mat d[`i',4] = r(min)
	mat d[`i',5] = r(max)
			
	local i = `i'+1
	
	}
	
mat rownames d = `namelist'				// use namelist as rownames

// frmttable as output in Stata
frmttable, stat(d)

// there are many options for nice formatting, this is for the decimal points
frmttable, stat(d) sdec(0,0,0,0,0)

// exporting works the same, just add "using" and the filename
frmttable using "Demographics.rtf", stat(d) sdec(0,0,0,0,0)

// don't forget to add "replace" if the file already exists			
			

The flexibility of frmttable

While the above code might look as the previous table commands, just more complicated, it gives you a great flexibility. For example, you can combine summary statistics and regression results.

sysuse auto, clear

// use tabstat to obtain a matrix with the summary statistics you need
tabstat mpg rep78 weight length, stats(mean min max) save
matrix A = r(StatTotal)

// as above, fill a matrix with results - now with regression results 
// we need one row for the coefficient and one for the standard error
matrix B = J(2,4,.)

local i = 1

foreach var of varlist mpg rep78 weight length {
	
	reg price `var'
	
	mat B[1,`i'] = _b[`var']		// this is a shortcut to get to the coefficient estimate
	mat B[2,`i'] = _se[`var']		// this is a shortcut to get to the s.e. estimate
	
	local i = `i'+1
	
}

// first, take the summary statistics
frmttable, stat(A)

// then, add the regression results using "append"
frmttable, stat(B) rtitles("coefficient"\"s.e.") append

// frmttable remembers its last content, so you can simply export by typing
 frmttable using "Demographics_and_regression.rtf"