Announcement

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

  • Trouble working with %tc dates

    Hello,

    I have a variable "EndDate" whose format is double yet displays as if it was %tc. I need to identify the maximum date by categories of the group "id" using this variable. After formatting the variable to %tc, the "bys id: egen max" function calculates a different %tc date compared to any of the original dates within any single id. I haven't been able to solve the problem. Thank you.

    format EndDate %tc
    bys id: egen maxenddate=max(EndDate)
    format maxenddate %tc

    id EndDate maxenddate
    1 01feb2023 10:51:32 01feb2023 18:00:33
    1 01feb2023 18:00:14 01feb2023 18:00:33
    1 31jan2023 06:45:08 01feb2023 18:00:33

  • #2
    Note that double is a variable or storage type, not a format. %tc is a display format.

    See e.g.

    Code:
    help data_types
    
    help format
    In most instances variable or storage type and display format are not yoked closely, except that numeric variables require numeric formats and string variables require string formats.

    But date-times with %tc display format have units milliseconds and you get into a mess if you try to store them in anything but a double.

    That is what you are trying to do, by accident. See

    Code:
    help datetime
    for repeated admonitions to use double and only double for any datetime. Hence your egen call should start

    Code:
    bysort id : egen double maxenddate = max(EndDate)

    Comment


    • #3
      Thank you Nick, very clear and solved the problem.

      Comment

      Working...
      X