Quick Table for Converting Different Dates to Stata Format
Quick Table for Converting Different Dates to Stata Format
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)) |
Related Posts
Source: https://fintechprofessor.com/2018/10/22/quick-table-for-converting-different-dates-to-stata-format/
Posted by: horacelickeye0194165.blogspot.com
10 Comments
A very useful and concise post on Stata dates. I shall add it to my favorites and come back for quick reference when need to convert dates in Stata.
Sir aoa how can v get t values in outreg2 command?
You can use asdoc for regression output. asdoc provides three different regression outputs. The standard outreg2 regression output can be generated by asdoc as shown in this post For t-statistics, use the option rep(t)
This is very helpful, thanks!
What if you have multiple date formats in the same dataset (e.g. 1/2/1974, 1-2-1974, 2-1-1974, 1974, nineteen seventy four, etc) and need to standardize them to just the four-digit birth year to calculate age?
Chris,
If you have all these dates in the same column, then you can extract the date or year separately from each format. Where the format does not match the code, the newly created variable shall then contain missing values. We would then use the replace command with the if condition to fill the missing values using again a matching format. For example
i have a numeric data 2010q1. please separate year and quarter
Abaid, you can use this code:
I have a "2/1/2020 6:00". How can i extract the month?