Epidemiology & Technology

Stata Date – Time calculations

Stata stores and saves date and time as numeric intervals referenced from 1 Jan 1960 (01jan1960 = 0)

When the variable is time, one should use double variable type

When variable is date, float is OK

Stata help

help datetime
help datetime_translation
help datetime_display_formats

The above command is your best resource!

Stata Internal Format (SIF) and Display Formats for time data

Stata stores time data with specific stata internal formats (datetime/c, date etc.) which can be formatted for display using display formats (%tC, %td etc.)

  • %tc = datetime/c – milliseconds – use “double” precision
  • %tC = datetime/C – milliseconds adjusted for leap seconds (UTC) – use “double” precision
  • %td = date – days since 01jan1960
  • %tw = weekly date – weeks since 1960w1
  • %tm = monthly date – months since 1960m1
  • %tq = quarterly date – quarters since 1960q1
  • %th = half-yearly date – half-years since 1960h1
  • %ty = yearly date – years since 0000

Code examples of creating date-time variables in stata

Date

gen dateOfVisit= date("18/8/2010", "DMY")
format dateOfVisit  %td

# String variable in dataset - dov , with dates written as 18/8/2010, 18-8-2020, etc
gen dateOfVisit= date(dov, "DMY")
format dateOfVisit  %td

# Three numeric variables in dataset month, date and year 
gen eventdate = mdy(month, date, year)
format eventdate %td

# Two numeric variables in dataset , date and year and one String variable month
# Convert to one concatenated string variable and the convert to SIF
gen str dateOfVisit_temp = month + " " + str(date) + " " + str(year)
gen dateOfVisit= date(dateOfVisit_temp , "MDY")
format dateOfVisit  %td


# Convert Stata Time variable "timeOfVisit" formated as % tc to a Stata Date variable
gen dateOfVisit = dofc(timeOfVisit)
format eventdate %td


# Get date of surgery, given you know date of follow-up  and 
# time between follow-up and surgery (followUpafterDays)
gen dateOfVisit= date(dov, "DMY")
format dateOfVisit  %td
gen dateOfSurgery = dateOfVisit= - followUpafterDays
format dateOfSurgery %td
Code language: PHP (php)

Extracting components from a Stata date / time variable

# Which Day of Week (DoW) was surgery done ?
gen dayOfSurgery = dow(dateOfSurgery)

# Which Day of month (DoW) was surgery done ?
gen dayOfSurgery = day(dateOfSurgery)
# OR
gen int dayOfSurgery = dateOfSurgery- dofm(mofd(dateOfSurgery)) + 1
# https://www.statalist.org/forums/forum/general-stata-discussion/general/1425117-month-week-number

Code language: PHP (php)

Weeks in Stata

Nick Cox writes on Stata List - https://www.stata.com/statalist/archive/2012-06/msg01322.html

A Stata week starts on Sunday (dow for Sundays = 0)

Week 1 of any given calendar year begins on 1 January of that
year, regardless. So week 2 starts on 8 January, and so forth. 

There are only 52 weeks of the year, regardless. So day 365
of a year is always assigned to week 52. And day366 is also so
assigned whenever it occurs, within a leap year. So week 52 is 8 or 9
days long, always
Also see -  https://www.statalist.org/forums/forum/general-stata-discussion/general/1425117-month-week-number
# Which Week of Month was surgery done ?
gen week_in_month = ceil(day(dateOfSurgery)/7)
# OR
gen day_in_month = eventdate - dofm(mofd(dateOfSurgery)) + 1
gen week_in_month = ceil(day_in_month/7)

# Which Week of year was surgery done ?
gen week_in_month = week(dateOfSurgery)Code language: PHP (php)

gen int week_in_month = ceil(day(eventdate)/7)

Related Posts