help for tabex

Basic syntax

tabex var

or

tabex, reset

Advanced syntax

tabex var1 [var2] [if] [using filename], [cname(string)] [reset]

Description

tabex does a single variable tabulate and exports the result to an Excel document in your current
directory, or to a specified file path. It can export multiple tabs to the same spreadsheet.
tabex can also perform a sum of a numeric variable if you choose to include a second variable.

Remarks

Unless you specify another filename and path with using, tabex will create and modify an Excel document called
"tabex.xlsx" in your current directory. If you are going to do multiple sessions of tabs, take time to rename
this document after each session so it does not get overwritten.

Stata will keep track of how many tabex you have run and exports each one to the next available column in
the output file. You can tabex up to 50 times, after which Stata will return an error and tell you to reset tabex.

To reset tabex just type: tabex, reset or include the reset option in your 50th (or last)
tabex. Remember to rename or change your output file after resetting or your output will be overwritten!

Arguments and options

var1 is required unless you use the reset option. This var is basically your "by" or
"level" variable. tabex calculates a frequency within each unique value of this variable.

var2 is optional. This var must be numeric because tabex will sum this variable by var1.

[if] specifies which subset of your data you are interested in tabulating.

using is optional and will tell Stata where to save your tabex output.
If you do not specify a path and filename, Stata will create an Excel document
called "tabex.xlsx" in your current directory and export to that file.

cname(string) is optional and gives you a way to title each column in your output.
If you choose to forgo the cname() option, tabex will title the output column with
whatever letter corresponds to that column in Excel. It is highly recommended that you do not
forgo the cname() option, but if you must, please keep careful track of what you are
tabexing so you can properly label your output later.

[reset] is optional. It resets tabex back to the first column of the output file, kind of like
a typewriter. You MUST reset after 50 tabexes but you may reset earlier than that if you wish.
reset can be invoked either as an option with any tabex command or by itself by typing: tabex, reset.

Examples

sysuse nlsw88,clear

tabex age if collgrad == 0 using "nlsw_tabs.xlsx",cname(hs)
tabex age if collgrad == 1 using "nlsw_tabs.xlsx",cname(coll)
tabex age if married == 0 using "nlsw_tabs.xlsx",cname(single)
tabex age if married == 1 using "nlsw_tabs.xlsx",cname(married) reset

tabex industry using "nslw_ind_tabs.xlsx",cname(all)
tabex industry if union == 0 using "nslw_ind_tabs.xlsx",cname(non-union)
tabex industry if union == 1 using "nslw_ind_tabs.xlsx",cname(union)
tabex industry if union == . using "nslw_ind_tabs.xlsx",cname(miss_union)
tabex industry if collgrad == 0 & occupation != 3 using "nslw_ind_tabs.xlsx",cname(hs_non_sales)
tabex industry if collgrad == 0 & occupation == 3 using "nslw_ind_tabs.xlsx",cname(hs_sales) reset

Running the above code would output two Excel documents, one with four exported tabulates and one with six.
Each tabulate being a frequency, by age in the first example and industry in the second, based on if logic.

The reset options are used to insure that subsequent tabexes appear at the beginning of the output document.

Acknowledgements

Lyz Gaumer provided this problem and inspired the solution.