Announcement

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

  • Time discrepancies

    Hi STATA Experts,

    I could not figure out why Stata changes minutes and seconds when I try to create a common datetime variable based on the existing datetime variables using some condition.
    For example, I use this part of code to create a new variable <rand_datetime>

    gen rand_datetime=randold_dt if rand_before11nov==1
    replace rand_datetime=rand_dt if rand_before11nov !=1
    format rand_datetime %tc

    After running the above code for some records time (minutes and seconds do not natch with the original one)
    Could someone shed a light on this problem?
    I attached a screenshot of partial result and particular highlighted one as an example of discrepancy.

    Thanks

    Attached Files

  • #2
    Try
    Code:
    generate double rand_datetime = randold_dt if rand_before11nov == 1
    and see if that helps.

    Comment


    • #3
      Amplifying the on-target answer of Joseph Coveney I note that the advice that you must use double for datetimes appears repeatedly in

      Code:
      help datetime
      See also https://journals.sagepub.com/doi/pdf...6867X251341416 which may be interesting or useful as an overview of this territory.

      Comment


      • #4
        Brilliant! This works well. Thanks a lot Joseph and Nick. Very helpful as always.

        Comment

        Working...
        X