77
datetime — Date andtime values andvariables s 3
In the remaining text, we will use the following notation:
tc: a Stata double variable containing
SIF
datetime/c values
tC: a Stata double variable containing
SIF
datetime/C values
td: a Stata variable containing
SIF
date values
tw: a Stata variable containing
SIF
weekly date values
tm: a Stata variable containing
SIF
monthly date values
tq: a Stata variable containing
SIF
quarterly date values
th: a Stata variable containing
SIF
half-yearly date values
ty: a Stata variable containing
SIF
yearly date values
HRF-to-SIF conversion functions
Function to convert
SIF
type
HRF
to
SIF
Note
datetime/c
tc=
clock(HRFstr, mask) tc must be double
datetime/C
tC=
Clock(HRFstr, mask) tC must be double
date
td=
date(HRFstr, mask) td may be float or long
weekly date
tw=
weekly(HRFstr, mask) tw may be float or int
monthly date
tm=
monthly(HRFstr, mask) tm may be float or int
quarterly date
tq=
quarterly(HRFstr, mask) tq may be float or int
half-yearly date
th= halfyearly(HRFstr, mask) th may be float or int
yearly date
ty=
yearly(HRFstr, mask) ty may be float or int
Warning: To prevent loss of precision, datetime
SIF
smust be stored as doubles.
Examples:
1. You have datetimes stored in the string variable mystr, an example being “2010.07.12
14:32”. To convert to
SIF
datetime/c, you type
. gen double eventtime = clock(mystr, "YMDhm")
The mask "YMDhm" specifies the order of the datetime components. In this case, they are
year, month, day, hour, and minute.
2. You havedatetimes stored in mystr, an examplebeing “2010.07.1214:32:12”. You type
. gen double eventtime = clock(mystr, "YMDhms")
Mask element s specifies seconds. In example 1, there were no seconds; in this example,
there are.
3. You have datetimes stored in mystr, an example being “2010 Jul 12 14:32”. You type
. gen double eventtime = clock(mystr, "YMDhm")
This is the same command thatyou typed in example 1. In the mask, you specify the order
of thecomponents; Statafiguresout the style for itself. In example1, months were numeric.
In this example, they are spelled out (and happen to be abbreviated).
68
4 datetime—Date andtime valuesandvariables
4. You have datetimes stored in mystr, an example being “July 12, 2010 2:32 PM”. You
type
. gen double eventtime = clock(mystr, "MDYhm")
Stata automatically looks for
AM
and
PM
, in uppercase and lowercase, with and without
periods.
5. You have datetimes stored in mystr, an examplebeing “7-12-10 14.32”. The 2-digit year
is to be interpreted as being prefixed with 20. You type
. gen double eventtime = clock(mystr, "MD20Yhm")
6. You have datetimes stored in mystr, an example being “14:32 on 7/12/2010”. You type
. gen double eventtime = clock(mystr, "hm#MDY")
The # sign between m and M means, “ignore one thing between minute and month”, which
in this case is the word “on”. Had you omitted the # from the mask, the new variable
eventtime would have contained missing values.
7. You have a date stored in mystr, an example being “22/7/2010”. In this case, you want
to create an
SIF
date instead of a datetime. You type
. gen eventdate = date(mystr, "DMY")
Typing
. gen double eventtime = clock(mystr, "DMY")
would have worked, too. Variable eventtime would contain a different coding from that
contained by eventdate; namely, it would contain milliseconds from 1jan1960 rather than
days (1,595,376,000,000 rather than 18,465). Datetime value 1,595,376,000,000 corresponds
to 22jul2010 00:00:00.000.
See[
D
]datetimetranslationformoreinformationaboutthe
HRF
-to-
SIF
conversion functions.
Displaying SIFs in HRF
Display format to
SIF
type
present
SIF
in
HRF
datetime/c
%tc
datetime/C
%tC
date
%td
weekly date
%tw
monthly date
%tm
quarterly date
%tq
half-yearly date
%th
yearly date
%ty
The display formats above are the simplest forms of each of the
SIF
s. You can control how each
type of
SIF
date is displayed; see[
D
]datetimedisplayformats.
68
datetime — Date andtime values andvariables s 5
Examples:
1. You havedatetimes stored instring variablemystr, anexamplebeing“2010.07.1214:32”.
To convert to
SIF
datetime/c and make the new variable readable when displayed, you type
. gen double eventtime = clock(mystr, "YMDhm")
. format eventtime %tc
2. You have adatestored in mystr, an example being “22/7/2010”. To convert to an
SIF
date
and make the new variable readable when displayed, you type
. gen eventdate = date(mystr, "DMY")
. format eventdate %td
Building SIFs from components
Function to build
SIF
type
from components
datetime/c
tc= mdyhms(M, D, Y, h, m, s)
tc= dhms(td, h, m, s)
tc= hms(h, m, s)
datetime/C
tC = Cmdyhms(M, D, Y, h, m, s)
tC = Cdhms(td, h, m, s)
tC = Chms(h, m, s)
date
td = mdy(M, D, Y)
weekly date
tw= yw(Y, W)
monthly date
tm= ym(Y, M)
quarterly date
tq= yq(Y, Q)
half-yearly date
th= yh(Y, H)
yearly date
ty= y(Y)
Warning:
SIF
sfor datetimes must be stored as doubles.
Examples:
1. Your dataset has three variables, mo, da, and yr, with each variable containing a date
component in numeric form. To convert to
SIF
date, you type
. gen eventdate = mdy(mo, da, yr)
. format eventdate %td
2. Your datasethas two numeric variables, mo and yr. To convertto
SIF
date corresponding to
the first day of the month, you type
. gen eventdate = mdy(mo, 1, yr)
. format eventdate %td
3. Your dataset has two numeric variables, da and yr, and one string variable, month,
containing the spelled-out month. In this case, do not use the building-from-component
functions. Instead, construct a new string variable containing the
HRF
and then convert the
string using the
HRF
-to-
SIF
conversion functions:
. gen str work = month + " " + string(da) + " " + string(yr)
. gen eventdate = date(work, "MDY")
. format eventdate %td
VB Imaging - VB Code 128 Generation Guide PointF(100F, 100F)) docx.Save("C:\\Sample_Barcode.pdf"). All the properties above are included in the 96 'set rotation barcode.Rotate = Rotate.Rotate0 barcode
rotate individual pdf pages reader; how to rotate one page in pdf document C# Word - Sort Word Pages Order in C#.NET new order for all pages // 1. the length of the array MUST BE equal to pageCount // 2. each page index SHOULD be in the array and only once // otherwise, the
rotate pages in pdf and save; permanently rotate pdf pages
79
6 datetime — Date and time values and variables
SIF-to-SIF conversion
To:
From:
datetime/c
datetime/C
date
datetime/c
tC= Cofc(tc)
td = dofc(tc)
datetime/C
tc= cofC(tC)
td = dofC(tC)
date
tc= cofd(td)
tC= Cofd(td)
weekly
td= dofw(tw)
monthly
td= dofm(tm)
quarterly
td= dofq(tq)
half-yearly
td= dofh(th)
yearly
td= dofy(ty)
To:
From:
weekly
monthly
quarterly
date
tw= wofd(td)
tm= mofd(td)
tq= qofd(td)
To:
From:
half-yearly
yearly
date
th= hofd(td)
ty= yofd(td)
To convertbetweenmissingentries, usetwofunctions,goingthroughdateordatetimeasappropriate.
For example, quarterly of monthly is tq= qofd(dofm(tm)).
Examples:
1. You have the
SIF
datetime/c variable eventtime and wish to create the new variable
eventdate containing just the date from the datetime variable. You type
. gen eventdate = dofc(eventtime)
. format eventdate %td
2. You havethe
SIF
datevariableeventdate and wish to createthenew
SIF
datetime/cvariable
eventtime from it. You type
. gen double eventtime = cofd(eventdate)
. format eventtime %tc
The time components of the new variable will be set to the default 00:00:00.000.
3. You have the
SIF
quarterly variable eventqtr and wish to create the new
SIF
date variable
eventdate from it. You type
. gen eventdate = dofq(eventqtr)
. format eventdate %tq
The new variable, eventdate, will contain 01jan dates for quarter 1, 01apr dates for
quarter 2, 01jul dates for quarter 3, and 01oct dates for quarter 4.
4. You have the
SIF
datetime/c variable admittime and wish to create the new
SIF
quarterly
variable admitqtr from it. You type
. gen admitqtr = qofd(dofc(admittime))
. format admitqtr %tq
Because there is no qofc() function, you use qofd(dofc()).
81
datetime — Date andtime values andvariables s 7
Extracting time-of-day components from SIFs
Desired component
Function
Example
hour of day
hh(tc) or hhC(tC)
14
minutes of day
mm(tc) or mmC(tC)
42
seconds of day
ss(tc) or ssC(tC)
57.123
Notes:
0 hh(tc) 23; 0 hhC(tC) 23
0 mm(tc) 59; 0 mmC(tC) 59
0 ss(tc)< 60; 0 ssC(tC)< 61
(sic)
Example:
1. You have the
SIF
datetime/c variable admittime. You wish to create the new variable
admithour equal to the hour and fraction of hour within the day of admission. You type
. gen admithour = hh(admittime) + mm(admittime)/60 + ss(admittime)/3600
Extracting date components from SIFs
Desired component
Function
Example*
calendar year
year(td)
2013
calendar month
month(td)
7
calendar day
day(td)
5
day of week
dow(td)
2
(0=Sunday)
Julian day of year
doy(td)
186
(1=first day)
week within year
week(td)
27
(1=first week)
quarter within year
quarter(td)
3
(1=first quarter)
half within year
halfyear(td)
2
(1=first half)
*All examples are with td=mdy(7,5,2013).
All functions require an
SIF
date as an argument. To extract components from other
SIF
s,
use the appropriate
SIF
-to-
SIF
conversion functiontoconvert toan
SIF
date, for example,
quarter(dofq(tq)).
Examples:
1. You wish to obtain the day of week Sunday, Monday, :::, corresponding to the
SIF
date
variable eventdate. You type
. gen day_of_week = dow(eventdate)
Thenew variable, day
of
week, contains 0 for Sunday, 1 for Monday,:::, 6 for Saturday.
63
8 datetime — Date and time values and variables
2. Youwish to obtain thedayofweekSunday,Monday,:::, correspondingtothe
SIF
datetime/c
variable eventtime. You type
. gen day_of_week = dow(dofc(eventtime))
3. Youhavethe
SIF
datevariableevdateandwishtocreatethenew
SIF
datevariableevdate
r
from it. evdate
rwill contain the same date as evdate but rounded back to the first of
the month. You type
. gen evdate_r = mdy(month(evdate), 1, year(evdate))
In theabovesolution,weused thedate-componentextractionfunctionsmonth()and year()
and used the build-from-components function mdy().
Conveniently typing SIF values
You can type
SIF
values by just typing the number, such as 16,237 or 1,402,920,000,000, as in
. gen before = cond(hiredon < 16237, 1, 0) if !missing(hiredon)
. drop if admittedon < 1402920000000
Easier to type is
. gen before = cond(hiredon < td(15jun2004), 1, 0) if !missing(hiredon)
. drop if admittedon < tc(15jun2004 12:00:00)
You can type
SIF
date values by typing the date inside td(), as in td(15jun2004).
You can type
SIF
datetime/c values by typing the datetime inside tc(), as in tc(15jun2004
12:00:00).
td()and tc()arecalledpseudofunctionsbecausetheytranslatewhatyou typeintotheir numerical
equivalents. Pseudofunctions require only that you specify the datetime components in the expected
order, soratherthan15jun2004 above, wecouldhavespecified 15 June2004,15-6-2004, or 15/6/2004.
The
SIF
pseudofunctions and their expected component order are
Desired
SIF
type
Pseudofunction
datetime/c
tc([day-month-year] hh:mm[:ss[.sss]])
datetime/C
tC([day-month-year] hh:mm[:ss[.sss]])
date
td(day-month-year)
weekly date
tw(year-week)
monthly date
tm(year-month)
quarterly date
tq(year-quarter)
half-yearly date
th(year-half)
yearly date
none necessary; just type year
The day-month-year in tc() and tC() are optional. If you omit them, 01jan1960 is assumed.
Doing so produces time as an offset, which can be useful in, for example,
. gen six_hrs_later = eventtime + tc(6:00)
64
datetime — Date andtime values andvariables s 9
Obtaining and working with durations
SIF
valuesaresimplydurationsfrom 1960.
SIF
datetime/cvalues record thenumber ofmilliseconds
from 1jan1960 00:00:00;
SIF
date values record the number of days from 1jan1960, and so on.
To obtain the time between two
SIF
variables—the duration—subtract them:
. gen days_employed = curdate - hiredate
. gen double ms_inside = discharge_time - admit_time
To obtain a new
SIF
that is equal to an old
SIF
before or after some amount of time, just add or
subtract the desired durations:
. gen lastdate = hiredate + days_employed
. format lastdate %td
. gen double admit_time = discharge_time - ms_inside
. format admit_time %tc
Remember to use the units of the
SIF
variables.
SIF
dates are in terms of days,
SIF
weekly dates
are in terms of weeks, etc., and
SIF
datetimes are in terms of milliseconds. Concerning milliseconds,
it is often easier to use different units and conversion functions to convert to milliseconds:
. gen hours_inside = hours(discharge_time - admit_time)
. gen admit_time = discharge_time - msofhours(hours_inside)
. format admit_time %tc
Function hours() converts milliseconds to hours. Function msofhours() converts hours to
milliseconds. The millisecond conversion functions are
Function
Purpose
hours(ms)
convert milliseconds to hours; returns ms=(60601000)
minutes(ms)
convert milliseconds to minutes; returns ms=(601000)
seconds(ms)
convert milliseconds to seconds; returns ms=1000
msofhours(h)
convert hours to milliseconds; returns h60601000
msofminutes(m)
convert minutes to milliseconds; returns m601000
msofseconds(s)
convert seconds to milliseconds; returns s 1000
If you plan on using returned values to add to or subtract from a datetime
SIF
,be sure they are
stored as doubles.
Using dates and times from other software
Most softwarestores dates and timesnumerically as durations fromsomesentinel date in specified
units, but they differ on the sentinel date and the units. If you have imported data, it is usually
possible to adjust the numeric date and datetime values to
SIF
.
75
10 datetime — Date and time values and variables
Converting
SAS
dates:
SAS
provides dates measured as the number of days since 01jan1960. This is the same coding
as used by Stata:
. gen statadate = sasdate
. format statadate %td
SAS
provides datetimesmeasured as thenumber ofsecondssince01jan1960 00:00:00, assuming
86,400 seconds/day. To convert to
SIF
datetime/c, type
. gen double statatime = (sastime*1000)
. format statatime %tc
It is important that variables containing
SAS
datetimes, such as sastime above, be imported
into Stata as doubles.
Converting
SPSS
dates:
SPSS
providesdatesanddatetimesmeasured asthenumberofsecondssince14oct158200:00:00,
assuming 86,400 seconds/day. To convert to
SIF
datetime/c, type
. gen double statatime = (spsstime*1000) + tc(14oct1582 00:00)
. format statatime %tc
To convert to
SIF
date, type
. gen statadate = dofc((spsstime*1000) + tc(14oct1582 00:00))
. format statadate %td
Converting R dates:
Rstores dates as days since 01jan1970. To convert to
SIF
date, type
. gen statadate = rdate - td(01jan1970)
. format statadate %td
Rstores datetimesasthenumber of
UTC
-adjustedsecondssince01jan1970 00:00:00. To convert
to
SIF
datetime/C, type
. gen double statatime = rtime - tC(01jan1970 00:00)
. format statatime %tC
To convert to
SIF
datetime/c, type
. gen double statatime = cofC(rtime - tC(01jan1970 00:00))
. format statatime %tc
There are issues of which you need to be aware when working with datetime/C values; see
Why there are two SIF datetime encodings s and Advice on using datetime/c and datetime/C,
both in[
D
]datetimetranslation.
Converting Excel dates:
If you have data in an Excel format file, you may want to use the import excel command.
If the Excel file contains numerically encoded dates, import excel will read those dates and
properly code them in
SIF
.You do not need to perform any conversion after importing your
data with import excel.
On the other hand, if you copy and paste a spreadsheet into Stata’s editor, dates and datetimes
are pasted as strings in
HRF
. The discussion below concerns converting such
HRF
datetime
strings to
SIF
numeric values.
Documents you may be interested
Documents you may be interested