Stata4Econ
Reusable Stata Code from Various Projects
Install / Use
/learn @FanWangEcon/Stata4EconREADME
This is a work-in-progress website of sample Stata files, produced by Fan. Materials gathered from various projects in which STATA code is used. The goal of this repository is to make it easier to find/re-use codes produced for various projects.
STATA files are linked below by section. Various functions are stored in corresponding .do files. To use the files, clone the repository. Some files have examples/instructions created using Jupyter notebooks or STATA translator and are shown as HTML and PDF files. See here for Github set up.
From Fan's other repositories: For dynamic borrowing and savings problems, see Dynamic Asset Repository; For example R code, see R Panel Data Code, for example Matlab code, see Matlab Example Code; For intro econ with Matlab, see Intro Mathematics for Economists, and for intro stat with R, see Intro Statistics for Undergraduates. See here for all of Fan's public repositories.
Please contact FanWangEcon for issues or problems.
1. Regressions
1.1 All Purpose Regression Tool
- All Purpose N Columns M Panels Regression Structure
- Code: DO | PDF Gen Tables Log | HTML Gen Tables Log
- Output: PDF Table | HTML Table | DOC Table
- A regression table has these ingredients: (1) regression method (2) LHS (3) RHS (to keep) (4) RHS (controls not to show in table) (5) conditions (6) regression options (7) row and column title and footnotes labeling
- Specify regression table column and panel specific ingredients for all, none, any row or column subsets freely
- Versatile structure that can test large sets of regression specifications
1.2 Various Examples
- Discrete Interactions: DO | HTML | PDF
- Regression with interacted discrete regressors
- core: regress + esttab
2. Generate Table
2.1 Multiple Regression Panels
- Multiple Panels: DO | Gen Table Log | PDF Table | TEX Table
- Three panels, different regressors for each panel, different conditionings each column.
- core: regres + esttab
2.2 Multiple Regression Panels with Interactions
- Continuous and Discrete Interactions: DO | Gen Table Log | PDF Table | TEX Table
- Three panels, different regressors for each panel, different conditionings each column.
- core: regres + esttab
2.3 Cross Tabulation
- Continuous and Discrete Interactions: DO | Gen Table Log | PDF Table | TEX Table
- Three row categories, two interacting column categories, statistics for multiple variables
- core: regres + estpost tabstat
3. Dataset Wrangling
3.1 Generate and Replace
- Generate Re-group Categorical Variable: DO | HTML | PDF
- Change or Reduce the number of categories for a categorical variable.
- 3 methods: a. recode; b. egen cut; c. inlist/inrange.
- Semi-automated loop: reset relabel regroup many variables with some time saving.
- core: recode turn (min/35 = 1 "Turn <35") ... (46/max = 5 "Turn > 45") (else =. ), gen(turn_m5); egen var = cut(turn), at(31(3)52) label; if inrange(turn, 31, 35), if inlist(turn, 46, 48, 51)
- Within-Group Fill and Replace Values: DO | HTML | PDF
- Fill missing values in group by single nonmissing value in group.
- core: bys trunk (var_one_val_in_group): gen var_test_fill = var_one_val_in_group[1]
- Labels: DO | HTML | PDF
- label a variable, label the values of a discrete variable
- output as one string (1) var label (2) var value labels (3) obs each value of categorical.
- core: labelbook + local st : variable label foreign + local st : label foreign_lab 0
3.2 Summary
- Multiple Variables Jointly Nonmissing: DO | HTML | PDF
- Find rows where there are no missing values for any variables in a list of variables and where certain conditionings are satisfied
- core: egen valid = rownonmiss($svr_list) if $scd_bse $scd_one $scd_two
3.3 Random
- Drop Random Subset of Data: DO | HTML | PDF
- Drop random subset of data for different variables
- core: *round((_n/N) x it_drop_frac) == round(it
Related Skills
node-connect
337.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.2kCommit, push, and open a PR
