Dates in Stata

Stata Date Formats

FormatDescriptionExampleNotes
%tcdate and time01jan1960 00:00:00.001 
%tCdate and time01jan1960 00:00:00.001accounts for  leap seconds
%tddate08sep1981 
%twweek1978w48 
%tmmonth1976m7 
%tqquarter1990q3 
%thhalf-year1980h2 
%tyyear 1983

Converting Dates to Stata Format

formatfrom stringsfrom numerical
components
%tcclock(string, mask)mdyhms(M, D, Y, h, m, s)
dhms(td, h, m, s)
hms(h, m, s)
%tCClock(string, mask)Cmdyhms(M, D, Y, h, m, s)
Cdhms(td, h, m, s)
Chms(h, m, s)
%tddate(string, mask)mdy(M, D, Y)
%twweekly(string, mask) yw(Y, W)
%tmmonthly(string, mask)ym(Y, M)
%tqquarterly(string, mask)yq(Y, Q)
%thhalfyearly(string, mask)yh(Y, H)
%tyyearly(string, mask)

Where mask is some combination of "MDYhms" denoting the order of the date components. For example, if my original date looked like "2010-03-14 01:59:26" then the mask would be "YMDhms." You need not include slashes, dashes or colons.

 

 

Working with dates

To extract components from a date, use the following functions. For example, if you would like to create a new variable which holds only the year from a date expression, use yofd(dateexpr).

from    to:%tc%tC%td%tw%tm %tq%th%ty
%tc--Cofcdofc      
%tCcofC--dofc      
%tdcofdCofd-- wofd mofdqofd hofdyofd
%tw dofw--     
%tm dofm --    
%tq dofq   --   
%th dofh    -- 
%ty dofy     --