datefix
Data ManagementConvert inconsistent date strings to Stata dates
Version 1.1.2 | 2026-08-11
datefix converts imported string date variables to numeric Stata daily dates for users cleaning data after import. It can infer date order, preserve raw values in a new variable, apply %td display formats, and identify values that prevent conversion.
Quick Start
Convert an imported day-month-year string in place:
clear
input str10 visit_date
"31/01/2020"
"15/02/2020"
"07/03/2020"
end
datefix visit_date
list
The command auto-detects the ordering and displays the converted variable with the default %tdCCYY/NN/DD format.
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 string dates; format or copy existing numeric daily-date values |
How It Works
datefixprocesses each variable invarlistindependently. String variables are parsed with Stata'sdate()function; numeric variables are treated as existing daily-date values and are formatted or copied.- If
order()is omitted, the command tries MDY, DMY, and YMD and selects the ordering with the most valid parses. MDY is preferred when tied for the top count; if YMD and DMY tie above MDY, YMD wins, so useorder()when the input is ambiguous. - Without
newvar(), a string variable is converted in place. Withnewvar(), the source variable is retained unlessdropis also specified. topyear()is passed todate()for interpreting two-digit years, anddf()changes the display format without changing the stored daily-date value.- Any nonmissing string that cannot be parsed causes the conversion to stop before that variable is replaced. The command reports missing-value counts before and after a successful conversion.
- A source variable with no nonmissing values stops with
r(2000)before any variable is changed, preventing a zero-result conversion from being reported as successful.
Worked Examples
1. Preserve the imported string and choose a display format
Use newvar() when the raw imported value should remain available for auditing.
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 visit_date vdate
2. Replace the source variable with a new name
Combine newvar() and drop when the cleaned variable should replace the imported column.
clear
input str10 admit_str
"06/15/2024"
"01/22/2024"
"11/03/2023"
end
datefix admit_str, newvar(admit_date) drop order(MDY) df(%tdDD/NN/CCYY)
list
3. Interpret two-digit years
Set topyear() explicitly when the source contains two-digit years.
clear
input str8 founded
"07/04/76"
"11/12/84"
"05/09/91"
end
datefix founded, order(MDY) topyear(1900)
list
4. Copy an existing numeric daily date with another format
Numeric input is useful when the values already count days from 01jan1960 but need a new variable or display format.
clear
input double visit_num
21915
21945
21988
end
datefix visit_num, newvar(visit_date) df(%tdDD_Mon._CCYY)
list
5. Diagnose values that block conversion
With diagnose, the command lists distinct unconvertible values, their frequencies, and the observation numbers where they occur before returning an error.
clear
input str12 dob
"2020/01/15"
"2020/00/15"
"2020/13/40"
"not recorded"
"2020/00/15"
end
capture noisily datefix dob, diagnose
The capture keeps the expected conversion error from stopping a larger do-file; the failing variable is not created or replaced when a nonmissing value fails.
Key Options
Syntax: datefix varlist [, newvar(name) drop df(%fmt) order(string) topyear(#) diagnose]. Use one or more source variables; newvar() is limited to a single source variable, and order() accepts MDY, DMY, or YMD (case-insensitive).
| Option | Default | Description |
|---|---|---|
newvar(name) |
Off | Create a new numeric date variable; retain the source unless drop is also specified |
drop |
Off | Drop the source variable when newvar() is used; without newvar(), it is redundant because strings are replaced and numeric inputs are formatted in place |
df(%fmt) |
%tdCCYY/NN/DD |
Apply a Stata daily-date display format; the format must begin with %td |
order(string) |
Auto | Parse strings as MDY, DMY, or YMD; automatic detection chooses the most valid ordering, with MDY preferred for top-count ties and YMD preferred over DMY when those two tie above MDY |
topyear(#) |
Off | Pass an integer top-year argument to date() for two-digit years |
diagnose |
Off | On string-conversion failure, list unconvertible values, frequencies, and observation numbers; the option may be abbreviated to diag |
Stored Results
datefix defines no stored results and clears incidental r() state before returning. Conversion messages, the auto-detected order (when order() is omitted), and missing-value counts are printed in the Results window.
Assumptions and Limits
- Numeric variables are assumed to contain Stata daily dates, measured as days from 01jan1960;
datefixdoes not validate their substantive meaning. - Datetime strings remain unsupported and fail conversion; colon-separated daily dates are accepted when they match the selected
order(). - A nonmissing string that fails under the chosen order rolls back the entire command, so every variable in a multi-variable
varlistremains unchanged. Usediagnoseto locate the failing values. - The diagnostic table shows at most 50 distinct failing values and at most 10 observation numbers per value. Displayed values longer than 30 characters are truncated, and the output indicates when additional values or rows are omitted.
- If date strings are ambiguous, automatic detection is heuristic; specify
order()for a known input convention.
References
- Stata
help date()documents the date-string parsing function used bydatefix;help datetime_display_formatsdocuments the%tddisplay formats accepted bydf().
QA
QA suites and how to run them are documented in qa/README.md.
Version History
- 1.1.2 (2026-08-11): Accepted colon-separated daily dates, rejected all-missing inputs without mutation, preserved literal Unicode diagnostic values, and made help examples and the local-install demo self-contained.
- 1.1.1 (2026-08-05): Made multi-variable conversion atomic and cleared incidental
r()state on return. - 1.1.0 (2026-06-25): Added the
diagnoseoption, which reports distinct unconvertible values, their frequencies, and observation numbers when conversion fails. - 1.0.1 (2026-06-19): Documentation fixes —
df()anddropnow render as options in the help file, added section markers, and standardized the author string. - 1.0.0 (2026-04-08): Initial release with auto-detection,
newvar(), custom display formats, andtopyear()support.
Author
Timothy P Copeland, Karolinska Institutet
License
MIT