STATA Data Analysis: Commands, Data Management, Regression and Panel Data
STATA is built around short, readable commands and reproducible do-files. This guide teaches the logic of a STATA workflow, from cleaning data to regression and panel models, and how to read what STATA tells you.
What is STATA?
STATA (officially "Stata") is statistical software widely used in economics, public health, political science and sociology. It combines menus with a powerful command language, strong data management and specialised methods for survey, panel and time-series data.
The STATA workflow
| Stage | Key commands |
|---|---|
| Set up | cd, log using, use, import delimited, import excel |
| Inspect | describe, codebook, summarize, tabulate, list in 1/10 |
| Clean and create | rename, generate, replace, recode, label define, label values, drop, keep |
| Combine | merge, append, reshape |
| Describe | summarize, detail, tabstat, tabulate var1 var2, row |
| Test | ttest, tabulate, chi2, pwcorr, sig |
| Model | regress, logit, probit, xtreg |
| Diagnose | predict, rvfplot, estat hettest, vif |
| Report | estimates store, esttab (user-written), margins |
The general syntax is command varlist if condition, options. For example, summarize income if female==1, detail.
Regression in STATA
regress wage education experience estimates an ordinary least squares (OLS) model. Read the output in this order:
- Number of observations and any dropped cases.
- F-test and R-squared: does the model explain variation overall?
- Coefficients: direction and size of each relationship, holding others constant.
- Standard errors, t and p-values, confidence intervals: precision of each estimate.
Add , robust for heteroskedasticity-robust standard errors, and use factor notation for categorical variables: regress wage education i.region.
Panel data
Panel data follow the same units (people, firms, countries) over time. Declare it with xtset id year.
| Model | Command | Assumption |
|---|---|---|
| Pooled OLS | regress y x |
Ignores the panel structure |
| Fixed effects | xtreg y x, fe |
Controls for all time-invariant differences between units |
| Random effects | xtreg y x, re |
Unit effects uncorrelated with predictors |
A Hausman test (hausman fe re after storing estimates) is commonly used to compare fixed and random effects, though theory and data context should also guide the choice.
Worked example
Illustrative data on 500 fictional employees.
use wages_demo, clear
describe
summarize wage education experience
generate lnwage = ln(wage)
regress lnwage education experience c.experience#c.experience i.female, robust
Suppose the coefficient on education is 0.082 (p < 0.001). Because the outcome is the log of wage, this means each additional year of education is associated with roughly 8% higher wages, holding experience and gender constant. The squared experience term allows the effect of experience to level off. The finding is an association: unobserved ability might affect both education and wages.
Common mistakes
- Working only in the menus, with no record of steps.
- Not checking for missing value codes (STATA treats
.as larger than any number, soif x > 100includes missing values unless you add& x < .). - Misreading log models (log-level, level-log and log-log coefficients mean different things).
- Using pooled OLS on panel data without justification.
- Forgetting robust or clustered standard errors where needed.
Explore learning resources
Worked examples, worksheets and checklists to use alongside this guide. They are learning tools, not answers to an assessment: adapt them to your own task and your institution’s requirements.
ReferenceSTATA command reference cardThe commands you will use most.
| Task | Command |
|---|---|
| Open data | use "file.dta", clear |
| Import CSV | import delimited "file.csv", clear |
| Overview | describe / codebook var |
| Summary | summarize var, detail |
| Frequency table | tabulate var |
| Cross-tab with chi-square | tabulate var1 var2, chi2 |
| New variable | generate newvar = expression |
| Change values | replace var = value if condition |
| Label values | label define yn 0 "No" 1 "Yes" then label values var yn |
| Regression | regress y x1 x2, robust |
| Declare panel | xtset id year |
| Fixed effects | xtreg y x1 x2, fe |
TemplateDo-file skeletonA tidy structure for any project.
* Project: ...
* Author: ... Date: ...
clear all
set more off
capture log close
log using "analysis.log", replace
use "raw.dta", clear
* 1. Inspect
* 2. Clean and create variables
* 3. Descriptive statistics
* 4. Models
* 5. Diagnostics
log closeWatch the topic explained
Video lesson · about 10 minutes · in production
STATA Walkthrough
An original lesson that teaches STATA with diagrams and a worked example, with captions and a transcript. These are the chapters it will cover; each links to the matching part of this guide.
- 01The STATA interface and do-files
- 02Inspecting and cleaning data
- 03Descriptive statistics and tables
- 04Running and reading regression
- 05Panel data with xtset and xtreg
Check your understanding
3 short questions. Choose an answer for each, then check. Every answer comes with an explanation.
Take your learning further
Learn STATA with a specialist Academic Expert
Sometimes a concept needs more than a written explanation. A live 1:1 session with an Academic Expert who works in STATA lets you ask your own questions, work through examples at your pace and get feedback on your own thinking.
What you can work on together
- Write clean, reproducible do-files
- Import, merge, reshape and clean datasets
- Run and interpret regression models
- Choose between fixed and random effects
- Diagnose problems such as heteroskedasticity
- Get feedback on your own results and write-up
How sessions help
- Make sense of your brief and marking criteria, so you know exactly what is being asked.
- Ask the questions you would not ask in a big class, as often as you need.
- Get timely, specific feedback on work you have written yourself.
- Practise explaining your reasoning out loud, ready for vivas, presentations and practicals.
- Extra support with academic English and conventions if English is not your first language.
Teach → coach → review. Experts explain, demonstrate and give feedback. You stay the author of your work and make the decisions in it.
Frequently asked questions
Is STATA hard to learn?
The command structure is consistent, so basic analysis is quick to learn. Advanced econometrics takes longer, mainly because of the statistics rather than the software.
Do I need STATA or can I use R?
Both can do most analyses. Use what your course, supervisor or field uses so you can get support.
Our academic integrity commitment
UniLearno supports learning, understanding and independent academic development. Our Academic Experts explain concepts, demonstrate methods, guide research and give feedback on work you have created. We do not complete assessed work, fabricate data or references, or produce anything for you to submit. You remain responsible for your own ideas, analysis, conclusions and final submission, and for following your institution’s academic integrity and AI-use rules.
Why a shortcut costs more than it saves
- It is more visible than it looks. Markers notice when work does not match what they know of you; document history, reference lists and follow-up conversations about your work are all used to check authorship.
- It creates a lasting risk. Services that sell completed assignments have threatened students afterwards, demanding money not to tell their university.
- The gap does not go away. Later units, supervised exams, vivas, placements and the job itself all assume you have the skill.
- Help that teaches is the safer route. Understanding the brief, getting feedback early and practising with an expert closes the gap for good.
Sources and about this guide
- StataCorp. Stata documentation (current version). stata.com/manuals.
- Acock, A. C. (2023). A gentle introduction to Stata (revised 6th ed.). Stata Press.
- Wooldridge, J. M. (2020). Introductory econometrics: A modern approach (7th ed.). Cengage.
- Written by
- UniLearno Learning Team
- Last reviewed
- Found an error?
- Tell us and we will check and correct it.