Announcement

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

  • Formatted numeric date variable is displaying in scientific notation

    I have a date variable (type: double, Format: %tcdd-Mon-CCYY_HH:MM:SS) that displays as "13-Jul-2023 12:31:02." I would like to reformat to just date time (%td) or even century month code. After reading stata files, I tried reformatting it with the following

    format surveystartdate %td

    It re-formats the variable, but it's only displayed as scientific notation (2.00e+12). What am I missing here?

    Thanks!
    Last edited by Kathleen Broussard; 15 Nov 2023, 11:24.

  • #2
    I have a date variable (type: double, Format: %tcdd-Mon-CCYY_HH:MM:SS) that displays as "13-Jul-2023 12:31:02."
    No, you don't. That's not a date variable. It's a date-time variable, or in Stata-speak, it's a clock variable. If you try to format it as if it were a date variable, you get, as you have seen, gibberish.

    So, the first thing you have to decide is whether you want to change the appearance of the variable in Stata outputs, or whether you wish to change the variable itself. So, if you still plan to use the variable in calculations with other clock variables, you might only want to tell Stata to leave the actual value of the variable alone but only show you the date. That would be done with -format surveystartdate %tcdd-Mon-CCYY-. If you only care about the month and year, -format surveystartdate %tcMon-CCYY- does that. But remember, if you do that, that you have not changed the actual contents of surveystartdate--it is still a clock variable and if you try to, say, compare it to a daily date variable, or a monthly date variable you will get nonsense.

    If you actually want to change the contents of surveystartdate so that it is truly a date variable, that requires a different approach altogether:
    Code:
    gen surveystartdateonly = dofc(surveystartdate)
    format surveystartdateonly %td
    You can then do calculations or comparisons with surveystartdateonly that involve other daily date variables.

    So the key point is that when working with dates and times in Stata, what you see is not necessarily what you get. Stata gives you the ability to control what parts of a date or date-time variable you wish to see in displays of results by using display formats. But doing that does not change the character or value of the variable.

    By the way, I think, because of the potential for this kind of confusion, it is a good practice to choose variable names that state what a variable really is. Since surveystartdate is not actually a Stata date variable, I wouldn't use that name. I would name it surveystartdttm, so that the name tells you it is a date-time variable. You are less likely to mistakenly treat it as a date variable when the name does not point you in that wrong direction.

    Comment


    • #3
      As a couple of extra ways of saying the same thing:

      Display format is what you see, not what is stored. Changing the display format never affects what is stored.

      Your example datetime is a number in trillions that is the number of milliseconds since the beginning of 1960.

      If you say to Stata that it is to be interpreted as a daily date, that daily date is billions of years into the future. Stata declines to try, without issuing an error message.

      For more on the general issue, see https://journals.sagepub.com/doi/pdf...867X1201200415

      Comment


      • #4
        Excellent! Thank you both for clarifying this.

        Comment

        Working...
        X