|
Note: This FAQ is for users of Stata 5, an older version of Stata. It
is not relevant for more recent versions.
Stata 5: How do I make date variables?
|
Title
|
|
Stata 5: Creating date variables
|
|
Author
|
Alan Riley, StataCorp
|
|
Date
|
January 1996
|
You can record dates anyway you want, but there is only one technique that
Stata understands, called an elapsed date. An elapsed date is the number of
days from January 1, 1960. In this format,
0 means January 1, 1960
1 January 2, 1960
-1 December 31, 1959
365 December 31, 1960
Stata provides functions to convert dates into elapsed dates, formats to
print elapsed dates in understandable forms, and other functions to
manipulate elapsed dates.
Conversion into elapsed dates
Two functions are provided: mdy() and date().
mdy() takes three numeric arguments — month, day, year —
and returns the corresponding elapsed date. For the following data,
month day year
7 11 1948
1 21 1952
11 2 1994
gen edate = mdy(month, day, year)
will produce the following data:
month day year edate
7 11 1948 -4191
1 21 1952 -2902
11 2 1994 12724
date(), on the other hand, takes two string arguments: the first is
the date to be converted, and the second informs date() of the order
of the month, day, and year. date() returns the corresponding elapsed
date. For the following data,
datestr1 datestr2
7-11-1948 11-7-1948
11.2.1994 2.11.1994
Sept11, 1994 11Sept1994
November 13, 1995 12 November 1995
gen edate1 = date(datestr1, "mdy")
gen edate2 = date(datestr2, "dmy")
will create two variables, edate1 and edate2, that contain the
same information :
datestr1 datestr2 edate1 edate2
7-11-1948 11-7-1948 -4191 -4191
11.2.1994 2.11.1994 12724 12724
Sept11, 1994 11Sept1994 12672 12672
November 13, 1995 12 November 1995 13100 13100
Displaying dates
Once converted to the elapsed date format, you can assign Stata's %d
format to the variable so that the dates are readable. Several
variations are possible for displaying dates. For the following data,
edate
-4191
12724
12672
13100
format edate %d
will change the display format to
edate
11jul48
2nov94
11sep94
13nov95
format edate %dM_d,_CY
will change the display format to
edate
January 11, 1948
November 2, 1994
September 11, 1994
November 13, 1995
Several other formats are possible. For online help, type help dates
in Stata, or see [U] 30 Commands for dealing with dates.
|