Daily Dates
Copying data from the internet, CSV files, or other sources into Stata will record the date as a string variable, shown with red color. Before we can use the Stata time-series or panel-data capabilities, we need to convert the string date to a Stata date. In the following table, the first column shows different date formats in which the date is already recorded and brought into Stata. To convert them into a Stata date, the example code is shown in the second column. Once the date is converted into a Stata readable format, we need to format the date so that the visual display of the date is human-readable. We can do that by using the %td format, for example, we can use the code format mydate %td
text | Code | Output |
---|---|---|
15-1-2015 |
gen mydate=date(text, "DMY") |
15feb2015 |
15/1/2015 |
gen mydate=date(text, "DMY") |
15feb2015 |
2015/1/15 |
gen mydate=date(text, "YMD") |
15feb2015 |
201502 |
gen mydate=date(text, "MY") |
1feb2015 |
1/15/08 |
gen mydate=date(text,"MDY",1999) |
15jan1908 |
1/15/08 |
gen mydate=date(text,"MDY",2019) |
15jan2008 |
1/15/51 |
gen mydate=date(text,"MDY",2000) |
15jan1951 |
1/15/01 |
gen mydate=date(text,"MDY",2050) |
15jan2001 |
1/15/00 |
gen mydate=date(text,"MDY",2050) |
15jan2000 |
20060125 |
gen mydate=date(text, "YMD") |
25jan2006 |
060125 |
gen mydate=date(text, "20YMD") |
25jan2006 |
Example using some data
* Enter example data
clear
input str9 text
"15/1/2015"
end
* Now convert the variable text to Stata date
gen mydate=date(text, "DMY")
* Change the display format
format mydate %td
From daily to other frequencies
From daily to | Code |
---|---|
weekly |
gen weekly_date = wofd(daily_date) |
Monthly |
gen monthly_date = mofd(daily_date) |
Quarterly |
gen qyarterly_date = qofd(daily_date) |
Yearly |
gen year = year(daily_date) |
Example using some data
* Enter example data
clear
input str9 text
"15/1/2015"
end
* Now convert the variable text to Stata date
gen daily_date=date(text, "DMY")
format daily_date %td
* Create a weekly date
gen weekly_date = wofd(daily_date)
format weekly_date %tw
* Create a monthly date
gen monthly_date = mofd(daily_date)
format monthly_date %tm
* Create a quarterly date
gen quarterly_date = qofd(daily_date)
format quarterly_date %tq
* Create a yearly date
gen year = year(daily_date)
From other frequencies to daily
If we already have dates in weekly, monthly, or quarterly frequencies, we can convert them back to daily dates. The second column in the following table provides an example of a given format in which the date is already recorded, and the third column presents the code which shall create a daily date. To see the codes in action, download this do file and execute. The file extension should be changed from doc to do after download.
From | given_date | Code |
---|---|---|
weekly |
2018w46 |
gen daily_date = dofw(given_date) |
Monthly |
2018m11 |
gen daily_date = dofm(given_date) |
Quarterly |
2018q4 |
gen daily_date = dofq(given_date) |
Yearly |
2018 |
gen daily_date = dofy(given_date) |
Complex Conversions
If we already have dates in weekly, monthly, or quarterly frequencies, we can convert them back to daily dates and then to other frequencies. The second column in the following table provides an example of a given format in which the date is already recorded, and the third column presents the code which shall convert the date to the desired frequency.
From | given_date | Code |
---|---|---|
Weekly to monthly |
2018w46 |
gen monthly_date = dofm(dofw(given_date)) |
Monthly to weekly |
2018m11 |
gen weekly_date = dofw(dofm(given_date)) |
Quarterly to monthly |
2018q4 |
gen monthly_date = dofm(dofq(given_date)) |
Monthly to quarterly |
2018m11 |
gen quarterly_date = qofd(dofm(given_date)) |
Weekly to quarterly |
2018w46 |
gen quarterly_date = qofd(dofw(given_date)) |
Quarterly to Weekly |
2018q4 |
gen weekly_date = dofw(dofq(given_date)) |