Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fixing date formats

    Hello,


    I am currently trying to merge two datasets based on the 'date' of the event. Problem is with the format of the date variable as they are not similar across the two datasets. I tried to find a solution based on the previous blog posts but unable to fix the issue. Therefore posting a brief summary of the issue below along the sample dataset and some solution would be helpful please.

    Based on the dataset below: The variable 'date1' and 'date2' are from datasets 1 and 2 respectively. I would like to convert both of these variables into the same format using which I would be able to merge the datasets.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str22 date1 float date2
    "20/01/2007" 17186
    "7/02/2007"  17204
    "30/03/2007" 17255
    "10/02/2007" 17207
    "14/02/2007" 17211
    "17/02/2007" 17214
    "17/02/2007" 17214
    "3/03/2007"  17228
    "17/02/2007" 17214
    "17/03/2007" 17242
    end
    format %td date2

    Regards
    Vignesh.

  • #2
    Code:
    gen date1_num = daily(date1,"DMY")
    format %td date1_num

    Comment


    • #3
      Format (meaning, display format) is not the issue. The issue is variable type.

      Code:
      gen ndate1 = daily(date1, "DMY") 
      format ndate1 %td
      should create a numeric date variable to compare with date2. Naturally within your first dataset you can call ndate1 date2 instead.

      Comment


      • #4
        The advice in posts #2 and #3 will solve your problem.

        I tried to find a solution based on the previous blog posts
        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment


        • #5
          Thanks Hemanshu and Nick for your help. I followed Nick Cox's code and instructions i.e. I fixed the format of the date variable in data set 1 and able to merge it with dataset 2.

          Regards
          Vignesh.

          Comment

          Working...
          X