Announcement

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

  • Date produced using cofd(td) sometimes, but not always, shows PREVIOUS date + 23 hrs and 59 seconds, instead of date at 00:00. Why?

    Hi all,

    I have a string variable, date_str. I want to get these dates into %tc format to enable merging and/or appending this dataset to another dataset for which the equivalent variable is in %tc format.

    I do this in two steps: 1) create date_td, a %td variable, based on date_str
    2) create date_tc, a %tc variable, based on date_td

    When I generate date_td, using the date() function in Stata 14.2, I get the expected results.

    But when I then go on to generate a datetime variable, date_tc, using the cofd() function with date_td, some of the dates in date_tc are moved one second earlier than expected, which shifts the date portion of the tc variable to one whole day earlier than the original variable.


    Code
    Code:
    *date_str is a str10 variable containing "YYYY-MM-DD"
    
    gen date_td= date(date_str,"YMD")
    
    format date_td %td
    
    gen date_tc = cofd(date_dt)
    
    format date_tc %tc
    Example of results
    date_str date_td date_tc dates_match
    6/16/2021 16-Jun-21 6/15/2021 23:59 NO
    8/12/2021 12-Aug-21 8/12/2021 0:00 YES
    8/26/2021 26-Aug-21 8/25/2021 23:59 NO
    6/16/2021 16-Jun-21 6/15/2021 23:59 NO
    10/6/2021 6-Oct-21 10/6/2021 0:00 YES
    5/22/2021 22-May-21 5/22/2021 0:00 YES
    Questions
    What is going on? Is this expected Stata behavior? If so, is there a way to generate a %tc variable where the date portion will always correspond to the date portion of the %td variable it is based on?
    Last edited by Abram Huyser-Honig; 26 Sep 2022, 15:08.

  • #2
    Date-time variables should be generated as double.

    See

    Code:
    help datetime
    where this is emphasized repeatedly.


    Comment


    • #3
      Thanks Nick!

      Comment

      Working...
      X