STATA for Biostatistics and Epidemiological Data Analysis
STATA is statistical software widely used for data management, biostatistics and epidemiology. Its command-based workflow supports transparent, repeatable analysis through do-files.
Learning objectives
- Understand the STATA interface and dataset structure.
- Import, label, clean and save health data.
- Produce descriptive statistics, tables and graphs.
- Perform common epidemiological tests and preserve reproducibility.
Main interface
- Command window: enter commands.
- Results window: view output.
- Variables window: inspect variable names and labels.
- Review window: view recent commands.
- Do-file Editor: write and rerun a documented analysis script.
Starting a project
clear all set more off cd "project-folder" import excel "healthdata.xlsx", firstrow clear describe codebook save "healthdata_clean.dta", replace
Use de-identified data and approved secure folders. Never overwrite the only raw dataset; keep raw, cleaned and analysis files separate.
Data inspection
describe, codebook, summarize, tabulate, list and misstable summarize.
Data management
generate, replace, recode, rename, label, sort, merge and append.
Descriptive analysis
| Purpose | Example command |
|---|---|
| Summarise age | summarize age, detail |
| Frequency table | tabulate sex |
| Cross-tabulation with chi-square | tabulate exposure outcome, chi2 row |
| Compare two means | ttest bp, by(treatment) |
| Correlation | pwcorr age bp, sig |
| Linear regression | regress bp age i.sex |
| Logistic regression | logistic outcome exposure age i.sex |
Epidemiological analysis
Commands such as cc and cs can analyse case-control and cohort/tabular data. Always define exposure, outcome, reference categories and study design before choosing a command. Report effect estimates with confidence intervals, not p-values alone.
Example workflow
use "healthdata_clean.dta", clear assert inrange(age,0,120) if !missing(age) tabulate outcome exposure, row chi2 logistic outcome exposure age i.sex estimates store model1 log close
The assert command checks a rule; modelling then estimates an adjusted association. Examine model assumptions, influential observations, confounding and interaction before interpretation.
Graphs
histogram age, normalexamines a distribution.graph bar (percent), over(outcome)displays proportions.scatter bp ageshows a numerical relationship.twoway line cases weekshows a time trend.
Reproducible practice
- Write commands in a do-file with comments.
- Open a log file to preserve output.
- Use relative project paths where possible.
- Record software version and installed packages.
- Set a random-number seed for simulations or sampling.
- Export final tables and graphs without manually changing results.
Review questions
- Why is a do-file preferable to entering commands only interactively?
- Which command creates a two-way table with a chi-square test?
- Differentiate
mergefromappend.
Return to the Clinical Medicine Year 2 curriculum
References
- StataCorp. Stata Base Reference Manual.
- StataCorp. Epidemiological Tables Reference Manual.
Educational content only; analysis of real patient data must follow ethical, privacy and institutional requirements.
