Custom Commands
A collection of small commands and utility scripts written over the years. Each
.ado file below has a matching .sthlp file, so
once both are saved to your Stata PERSONAL directory,
help commandname will work automatically.
Run sysdir in Stata to see where your
PERSONAL directory is, then drop the downloaded file(s) there.
Data Summaries & Cleaning
dsum
Quick cursory summary of a variable list: total observations, missing count,
% missing, and distinct values per variable, with an optional column for the sum.
dsum varlist [if] [in] [, sum]
- varlist - variables to summarize
- sum - adds a column with the sum of each numeric variable
Download dsum.ado ·
Help file
varcheck
Builds a table showing the percent of observations missing for each variable
in a varlist, broken out by another variable (e.g. survey wave or year). Useful for checking
data availability across waves of a longitudinal survey.
varcheck varlist, by(varname)
- varlist - variables to check
- by(varname) - variable to break the results out by
Download varcheck.ado ·
Help file
dummies
Creates a dummy variable for every distinct value of a string variable, cleaning
spaces out of the generated variable names.
dummies varname
- varname - string variable to expand into dummies
Download dummies.ado ·
Help file
mencode
A sometimes-useful stand-in for encode when the values and
their labels already exist side by side in the dataset. Reads the label text straight from the
data and applies it as a value label to the numeric variable.
mencode varname, labels(varname) [labelname(string) force drop]
- varname - variable to encode
- labels(varname) - variable holding the label text for each value
- labelname(string) - name for the value label (defaults to varname)
- force - overwrite an existing label with this name
- drop - drop the labels variable once applied
Download mencode.ado ·
Help file
mklist
Takes a variable's distinct values and concatenates them into a single delimited
string - useful for building an inlist() condition on the fly.
mklist varname [if] [in] [, delimiter(string) noquote]
- varname - variable to list distinct values of
- delimiter(string) - separator between values (defaults to a comma)
- noquote - don't wrap string values in quotes
Download mklist.ado ·
Help file
transpose
Quick-and-dirty transpose of the whole dataset, run through a CSV round-trip,
with the option to retain the first column as variable names.
transpose [, vars]
- vars - keep the first column as the new variable names
Download transpose.ado
ts
One-line utility that displays the current date and time - a quick way to time
how long a chunk of do-file code takes to run.
ts
Download ts.ado
Reshaping & Labels
supershape
An enhanced version of Stata's built-in reshape. Retains
variable labels when reshaping wide-to-long, or assigns them when going long-to-wide, and can
fill missing numeric values with zeros.
supershape wide|long stub, i(varlist) [j(varlist) keeplabels labels(varname) fill zeros string]
- wide|long - direction to reshape
- stub - stub name(s) of the variables being reshaped
- i(varlist) - identifying variable(s)
- j(varlist) - variable indicating column number (wide only)
- keeplabels - preserve variable labels when reshaping long
- labels(varname) - assign labels from this variable when reshaping wide
- fill / zeros - fill missing numeric values with zero
- string - reshape as string variables
Download supershape.ado ·
Help file
preserve_labels
collapse drops variable labels - this small wrapper
saves them beforehand and reapplies them after. Pass your collapse
statement in as a string.
preserve_labels "collapse (mean) mpg price weight, by(foreign)"
- collapse_statement - a full collapse command, passed in quotes
Download preserve_labels.ado ·
Help file