Stata Dates: Formatting, Converting, and Working with Date Variables
Stata dates are stored as numbers. Here's the complete date handling reference โ converting strings, display formats, date arithmetic, and tsset.
Your time-series model fails because dates look fine to you but Stata treats them as plain strings.
You will convert, format, and validate date variables correctly for tsset and panel analysis.
All examples tested in Stata 18 SE. Compatible with Stata 15+.
Quick Answer
- Convert string dates with `date()` or `monthly()` as appropriate.
- Apply display formats (`%td`, `%tm`) after conversion.
- Check conversion failures with `count if missing(datevar)`.
- Run `isid panel_id timevar` before tsset.
Build Date Variables That Work in Modeling Commands
Convert raw strings to daily and monthly dates
Date conversion errors often come from mixed source formats. Standardize masks and test for missing conversions immediately.
Once converted, format variables for readability without changing underlying numeric values.
If you are extending this pipeline, also review How to Merge Datasets in Stata and Stata predict: Postestimation Commands.
1clear all2input str10 raw_date str7 raw_month firm_id3"01/03/2020" "2020m03" 1014"15/04/2020" "2020m04" 1015"30/05/2020" "2020m05" 1026"12/06/2020" "2020m06" 1037end89gen date_d = date(raw_date, "DMY")10format date_d %td1112gen date_m = monthly(raw_month, "YM")13format date_m %tm1415count if missing(date_d) | missing(date_m)16list raw_date date_d raw_month date_m +----------------------------------------------+
| raw_date date_d raw_month date_m |
|----------------------------------------------|
1. | 01/03/2020 01mar2020 2020m03 2020m3 |
2. | 15/04/2020 15apr2020 2020m04 2020m4 |
3. | 30/05/2020 30may2020 2020m05 2020m5 |
4. | 12/06/2020 12jun2020 2020m06 2020m6 |
+----------------------------------------------+Prepare panel-time structures for tsset
Time-series commands need unique panel-time combinations. Duplicate dates within panel IDs will break lags and differences.
Use converted numeric date variables in tsset, not raw strings.
1clear all2input str10 raw_date str7 raw_month firm_id3"01/03/2020" "2020m03" 1014"15/04/2020" "2020m04" 1015"30/05/2020" "2020m05" 1026"12/06/2020" "2020m06" 1037end89gen date_d = date(raw_date, "DMY")10format date_d %td1112gen date_m = monthly(raw_month, "YM")13format date_m %tm1415count if missing(date_d) | missing(date_m)16list raw_date date_d raw_month date_m1718* ---- Section-specific continuation ----19gen year = year(date_d)2021* Example panel-time validation22isid firm_id date_m2324tsset firm_id date_m2526* Lag example27gen L1_wage = L.wage if !missing(wage) panel variable: firm_id (unbalanced)
time variable: date_m, 2020m3 to 2020m6
delta: 1 monthCommon Errors and Fixes
"type mismatch"
A date function or arithmetic operation was applied to a string date without numeric conversion.
Convert string dates first with date() or monthly(), then apply date formats and arithmetic.
type mismatch r(109);
gen next_day = raw_date + 1gen date_d = date(raw_date, "DMY")format date_d %tdgen next_day = date_d + 1format next_day %td1describe raw_date2gen date_d = date(raw_date, "DMY")3format date_d %td4gen week_after = date_d + 75format week_after %tdvariable name type format -------------------------------- raw_date str10 %10s
Command Reference
date / monthly / format
Stata docs โConverts string timestamps into numeric Stata date variables for time-series operations.
date(s,"DMY")Daily conversion with day-month-year maskmonthly(s,"YM")Monthly conversion from year-month stringsformat %td / %tmReadable date display formatsclock(s,"YMDhms")Datetime conversion for timestamp-level dataHow Sytra Handles This
Sytra can infer date masks from raw strings, generate conversion code, and validate panel-time uniqueness prior to tsset.
A direct natural-language prompt for this exact workflow:
Convert raw_date in DMY format and raw_month in YM format into numeric Stata dates, format them, check conversion failures, validate firm_id-date uniqueness, and tsset for panel analysis.Sytra catches these errors before you run.
Sytra can infer date masks from raw strings, generate conversion code, and validate panel-time uniqueness prior to tsset.
Join the Waitlist โFAQ
Why do Stata dates appear as large integers?
Stata stores dates as numeric offsets from 01jan1960. Apply display formats like %td or %tm to show human-readable dates.
How do I convert string dates to Stata dates?
Use date(stringvar, "DMY") or related masks, then apply format %td and verify missing conversions.
What is the best practice before tsset?
Ensure one observation per panel-time pair and convert dates to proper Stata numeric date variables before tsset.
Related Guides
- Stata String Functions: substr, strpos, regexm, and 30 More with Examples
- Importing Data into Stata: Excel, CSV, Fixed-Width, SAS, and SPSS
- GIS Data in Stata: Spatial Coordinates, Distance Features, and Regional Plots
- Clustered Standard Errors in Stata: vce(cluster) Explained with Examples
- Explore the data management pillar page
- Open the full data management guide index
- Browse all Stata & R guides on the blog index
- Browse all Stata pillars
We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.