datefix
Data ManagementConvert inconsistent date strings to Stata dates
Version 1.0.0 | 2026-04-08
datefix converts string date variables to numeric Stata daily dates and applies a daily-date display format. It is designed for the common cleanup step after import, especially when date order is inconsistent, two-digit years need disambiguation, or you want to preserve the original string alongside a cleaned numeric date.
Requirements
- Stata 16 or later
Installation
capture ado uninstall datefix
net install datefix, from("https://raw.githubusercontent.com/tpcopeland/Stata-Tools/main/datefix") replace
Commands
| Command | Description |
|---|---|
datefix |
Convert one or more string date variables to numeric Stata daily dates, optionally into a new variable with a custom display format |
Quick Start
If the imported dates are unambiguous, datefix can usually infer the correct ordering and replace the original string variable in place.
clear
input str10 visit_date
"31/01/2020"
"15/02/2020"
"07/03/2020"
end
datefix visit_date
list
In this example, datefix will auto-detect the ordering that produces the most valid parses, convert visit_date to a numeric daily date, and apply the default %tdCCYY/NN/DD format.
How It Works
- Without
newvar(), each variable invarlistis converted in place. - With
newvar(), only one source variable may be specified, and the original variable is preserved unlessdropis also requested. - If
order()is omitted,datefixtestsMDY,DMY, andYMDand uses the ordering that produces the most valid parses. If the strings are ambiguous, specifyorder()explicitly. topyear()passes Stata'stopyearargument todate()so two-digit years are interpreted correctly.- If a variable is already numeric,
datefixcan still copy it tonewvar()and apply a different daily-date display format.
Worked Examples
1. Auto-detect the ordering and replace the original variables
This is the default cleanup workflow for imported string dates.
clear
input str10 dob str10 dod
"31/01/2020" "02/02/2020"
"15/02/2020" "18/02/2020"
end
datefix dob dod
list
2. Preserve the original string and write a new formatted date variable
Use newvar() when you want to keep the raw imported string alongside the cleaned Stata date.
clear
input str10 visit_date
"03/14/2020"
"04/20/2020"
"05/01/2020"
end
datefix visit_date, newvar(vdate) order(MDY) df(%tdMonth_DD,_CCYY)
list
3. Handle two-digit years explicitly
When the source data use two-digit years, topyear() tells Stata how those years should be interpreted.
clear
input str8 founded
"07/04/76"
"11/12/84"
"05/09/91"
end
datefix founded, order(MDY) topyear(1900)
list
4. Copy an already numeric date variable and change only the display format
datefix also works as a lightweight formatting helper when the dates are already numeric Stata daily dates.
clear
input double visit_num
21915
21945
21988
end
format %td visit_num
datefix visit_num, newvar(visit_label) df(%tdDD_Mon._CCYY)
list
Common df() Formats
| Format string | Example output | Use |
|---|---|---|
%tdCCYY/NN/DD |
2020/01/10 | Default year-month-day display |
%tdDD/NN/CCYY |
10/01/2020 | Day-month-year display |
%tdMonth_DD,_CCYY |
January 10, 2020 | Full month name |
%tdDD_Mon._CCYY |
10 Jan. 2020 | Compact manuscript-style date |
For the full set of Stata date display formats, see help datetime_display_formats.
Practical Notes
newvar()cannot be combined with multiple source variables.dropis only meaningful whennewvar()is used.datefixstops when it encounters strings with:because those look like datetimes rather than daily dates.- Missing-value counts are reported before and after conversion so you can spot parsing problems quickly.
datefixdoes not store results inr().
Version History
- 1.0.0 (2026-04-08): Initial release with auto-detection,
newvar(), custom display formats, andtopyear()support.
Author
Timothy P Copeland, Karolinska Institutet