Useful STATA Commands
Note: Built-in commands are marked with an asterisk (*).
Tools
ftools: Mata commands for factor variables.gtools: A suite of commands that use hashes for a speedup over traditional stata commands.github: Searching, installing, and managing Stata packages from GitHub.halltool: A Stata toolbox (commands) developed by Hall.unique: Unique values of a variable or group of variables.egenmore: Extensions to commandgenerate.
Data Cleaning
winsor2: Winsorizing or Trimming variables. Devekloped by Yujun Lian.
String Functions
- *
substr(s, n1, n2): The substring ofs, starting atn1, for a length ofn2. - *
subinstr(s1, s2, s3, n): The firstnoccurrences ins1ofs2will replaced withs3. fdtavariables, from(s1) to(s2): Replacesiwiths2in allvariables.- *
regexm(s,re): Evaluates to 1 if regular expressionreis satisfied by the ASCII strings; otherwise, 0. - *
regexr(s, re, s2): Replaces the first substring within ASCII strings1that matches regular expressionrewith ASCII strings2.
Analysis & Regressions
reghdfe: Regression with High-Dimensional Fixed Effects.ppmlhdfe: Poisson Pseudo-Maximum Likelihood with High-Dimensional Fixed Effects.
Tabulation, Reporting, Visualization, and Documentation
fre: One-way frequency tables.esttab: Display and export formatted regression table.- Here is the global macro I frequently use for
esttaboptions.1
2
3
4
5
6global ESTTAB_OPT replace cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
stats(N r2_a, ///
label("Obs." "AR2" ) ///
fmt(%9.0fc %9.3f ) ///
) ///
star(* 0.1 ** 0.05 *** 0.01)
- Here is the global macro I frequently use for
- parmest: save coefficients and standard errors from regression results into a dta file.
Others
- *
which: Display location of an ado-file. adoedit: Open an ado-file.
Some usefule command blocks
Format long string variables
Format all long string variables to 30 characters while keeping the original format for short string variables.
1 | local chmax 30 |
If you have halltool installed, you can do this:
1 | fmtstr |
Loop for multiple files in a folder
1 | local folder_path = "./the_folder_path" |
Remove a folder and its contents
1 | local folder = "./the_folder_path" |
shell mv on MacOS is probably safer because you can resotre the folder from the Trash if you want to.
- Title: Useful STATA Commands
- Author: Xinya HAO (Hall)
- Created at : 2024-10-27 22:00:00
- Updated at : 2025-12-04 06:29:53
- Link: https://econometrics.club/2024/10/27/20241027_hallstatacmds/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments