Download Business Calendar For Stata

Skip to content
StataProfessor Logo

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

Attaullah Shah 2020-07-25T12:36:08+05:00

10 Comments

  1. Testobe Agar October 25, 2018 at 4:06 pm - Reply

    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.

  2. Shahzad Hussain October 25, 2018 at 11:29 pm - Reply
  3. Hamid Shah October 25, 2018 at 11:30 pm - Reply

    Sir aoa how can v get t values in outreg2 command?

    • Attaullah Shah October 25, 2018 at 11:33 pm - Reply

      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)

  4. Ye August 10, 2019 at 2:14 am - Reply

    This is very helpful, thanks!

  5. Chris July 9, 2020 at 7:10 pm - Reply

    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?

  6. Attaullah Shah July 10, 2020 at 9:07 am - Reply

    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

    clear input str34 dates "1/2/1974" "1-2-1974" "2-1-1974" "1974"     end  *Assuming that the first part of the date is the date, second is the month, third is the year.  *For extracting date gen datenum = date(dates, "DMY")  *Replace the missing values for dates that have only year part replace datenum = date(dates, "Y") if datenum == . format datenum %td  *To extract year gen year = year(date(dates, "DMY")) replace year = year(date(dates, "Y")) if year == . list        +-----------------------------+      |    dates   year     datenum |      |-----------------------------|   1. | 1/2/1974   1974   01feb1974 |   2. | 1-2-1974   1974   01feb1974 |   3. | 2-1-1974   1974   02jan1974 |   4. |     1974   1974   01jan1974 |      +-----------------------------+
  7. ABAID ULLAH October 23, 2020 at 12:07 pm - Reply

    i have a numeric data 2010q1. please separate year and quarter

  8. Attaullah Shah October 23, 2020 at 6:30 pm - Reply

    Abaid, you can use this code:

    gen year =  year(dofq(date)) gen quarter =  quarter(dofq(date))                          
  9. chi duong December 1, 2020 at 7:33 am - Reply

    I have a "2/1/2020 6:00". How can i extract the month?

Source: https://fintechprofessor.com/2018/10/22/quick-table-for-converting-different-dates-to-stata-format/

Posted by: horacelickeye0194165.blogspot.com

Post a Comment

Previous Post Next Post