Data Management
2026-02-2012 min read

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.

Sytra Team
Research Engineering Team, Sytra AI

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

  1. Convert string dates with `date()` or `monthly()` as appropriate.
  2. Apply display formats (`%td`, `%tm`) after conversion.
  3. Check conversion failures with `count if missing(datevar)`.
  4. 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.

dates-convert.do
stata
1clear all
2input str10 raw_date str7 raw_month firm_id
3"01/03/2020" "2020m03" 101
4"15/04/2020" "2020m04" 101
5"30/05/2020" "2020m05" 102
6"12/06/2020" "2020m06" 103
7end
8
9gen date_d = date(raw_date, "DMY")
10format date_d %td
11
12gen date_m = monthly(raw_month, "YM")
13format date_m %tm
14
15count if missing(date_d) | missing(date_m)
16list raw_date date_d raw_month date_m
. list 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 |
     +----------------------------------------------+
๐Ÿ’กFormats do not change values
Setting `%td` only changes display, not underlying numeric storage. This is why date arithmetic still works after formatting.

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.

dates-tsset.do
stata
1clear all
2input str10 raw_date str7 raw_month firm_id
3"01/03/2020" "2020m03" 101
4"15/04/2020" "2020m04" 101
5"30/05/2020" "2020m05" 102
6"12/06/2020" "2020m06" 103
7end
8
9gen date_d = date(raw_date, "DMY")
10format date_d %td
11
12gen date_m = monthly(raw_month, "YM")
13format date_m %tm
14
15count if missing(date_d) | missing(date_m)
16list raw_date date_d raw_month date_m
17
18* ---- Section-specific continuation ----
19gen year = year(date_d)
20
21* Example panel-time validation
22isid firm_id date_m
23
24tsset firm_id date_m
25
26* Lag example
27gen L1_wage = L.wage if !missing(wage)
. tsset firm_id date_m
       panel variable:  firm_id (unbalanced)
        time variable:  date_m, 2020m3 to 2020m6
                delta:  1 month
๐Ÿ‘Do not tsset on strings
tsset accepts numeric time variables. If your date is still string, lags and time operators will fail or behave unexpectedly.

Common 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.

. gen next_day = raw_date + 1
type mismatch
r(109);
This causes the error
wrong-way.do
stata
gen next_day = raw_date + 1
This is the fix
right-way.do
stata
gen date_d = date(raw_date, "DMY")
format date_d %td
gen next_day = date_d + 1
format next_day %td
error-fix.do
stata
1describe raw_date
2gen date_d = date(raw_date, "DMY")
3format date_d %td
4gen week_after = date_d + 7
5format week_after %td
. describe raw_date
variable 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.

gen d = date(strvar, "DMY") ; gen m = monthly(strvar, "YM") ; format d %td
date(s,"DMY")Daily conversion with day-month-year mask
monthly(s,"YM")Monthly conversion from year-month strings
format %td / %tmReadable date display formats
clock(s,"YMDhms")Datetime conversion for timestamp-level data

How 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:

sytra-prompt.txt
bash
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.


Written by Sytra Team
Research Engineering Team, Sytra AI

We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.

#Stata#Dates#Time Series#Data Management

Enjoyed this article?