Working with dates and times in Stata
You have dates/times
Stored in one variable With components stored
in multiple variables
String (text) variable Numeric variable
All components Some components
are numeric are numeric, others
1. Convert the string to a
Brought into Stata are strings
numeric date/datetime (A)
from a .csv or .txt file, 1. Build a numeric
Stata
with values date/datetime (D) 1. Create a string variable
2. Format the numeric formatted
representing those with all the components,
variable (B) date/datetime
used by another using string() to
2. Format the
software numeric variable convert the numeric
You want to
You want to extract a portion of (B) components to strings.
change units
the date/datetime 1. Convert the numeric
1. Use a date/datetime to a 2. Convert the string to a
1. Use an extraction function (E) conversion numeric variable numeric date/datetime
function (F) corresponding to Stata’s (A)
base date (C)
Numerically encoded dates/times brought into
Stata with import excel, import sas, or 2. Format the 3. Format the numeric
import spss will automatically be converted
2. Format the numeric variable (B)
new variable
to Stata dates/datetimes.
(B)
variable (B)
November 2, 2020 1
Units of measurement A. String-to-numeric conversions B. Display formats
Date type Units Date type Function Date type Format
Datetime Datetime clock(string, “mask”) Datetime %tc
Milliseconds since 01jan1960 [Link].000 Datetime(UTC) Clock(string, “mask”) Datetime(UTC) %tC
(assumes 86,400 s/day) Daily date date(string, “mask”) Daily date %td
Weekly date weekly(string, “mask”) Weekly date %tw
Datetime(UTC)
Monthly date monthly(string, “mask”) Monthly date %tm
Milliseconds since 01jan1960 [Link].000
Quarterly date quarterly(string, “mask”) Quarterly date %tq
(adjusted for leap seconds)
Daily date Days since 01jan1960 mask specifies the order of the date and time components,
Weekly date Weeks since 1960w1 and may contain any of the following: M, D, Y, 19Y, 20Y, Q, W,
Monthly date Months since 1960m1 h, m, s, and #.
Quarterly date Quarters since 1960q1
D. Building dates and times from components
Date type Function
Datetime mdyhms(M, D, Y, h, m, s)
dhms(e_d, h, m, s)
C. Converting dates and times from other software
hms(h, m, s)
SAS Date sasdate==statadate Datetime(UTC) Cmdyhms(M, D, Y, h, m, s)
Datetime sastime*1000 Cdhms(e_d, h, m, s)
dofc((spsstime*1000) + tc(14oct1582 00:00)) Chms(h, m, s)
SPSS Date
Datetime (spsstime*1000) + tc(14oct1582 00:00) Daily date mdy(M, D, Y)
Weekly date yw(Y, W)
R Date rdate – td(01jan1970) ym(Y, M)
Monthly date
Datetime rtime-tC(01jan1970 00:00) yq(Y, Q)
Quarterly date
Excel Date xldate + td(30dec1899)
Datetime round((xltime+td(30dec1899))*86400)*1000 e_d is a numerically encoded daily date.
2
E. Extraction functions To
F. Converting among units
Component Function From Datetime Datetime Daily
Year year(e_d) (UTC) date
Month month(e_d)
Day day(e_d) Datetime Cofc() dofc()
Day of week dow(e_d)
Datetime cofC() dofC()
Week w/in year week(e_d)
(UTC)
Quarter w/in year quarter(e_d)
Daily date cofd() Cofd()
e_d is a numerically encoded daily date.
Datetime Datetime (UTC)
Component Function Function
Hour of day hh(e_tc) hh(e_tC) To
Minutes of day mm(e_tc) mm(e_tC) From Daily Weekly date Monthly date Quarterly date
Seconds of day ss(e_tc) ss(e_tC) date
e_tc is a datetime variable and e_tC is a leap second- Daily date wofd() mofd() qofd()
adjusted datetime (UTC) variable.
Weekly dofw() mofd(dofw()) qofd(dofw())
date
Monthly dofm() wofd(dofm()) qofd(dofm())
date
Quarterly dofq() wofd(dofq()) mofd(dofq())
date