Announcement

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

  • view underlying numeric value of date

    Hi there
    Just wondering if there is a quick way to view the underlying numeric value of a date in a column that is formatted %td (without having to reformat the column).
    Any advice much appreciated. Thanks

  • #2
    you can do this using the -display- command; e.g., if you want to see the value in the first observation for a variable called date:
    Code:
    di date[1]

    Comment


    • #3
      Nice, thank you.
      It would be great if there were some quick way of toggling between the underlying numeric values and the formatted date values within the browse window, but perhaps that doesn't exist.

      Comment


      • #4
        The display format is not fixed. Assuming your date variable is named date, you can do the following:

        Code:
        gen date2= date
        browse date date2

        Comment


        • #5
          Changing the format of the variable [not column, please] is about the easiest way. Stata's bias is clear: you are expected to understand 2021m8 and not to care that Stata is working with 739 (I had to work it out before I could even write that).

          Comment


          • #6
            Hi,

            About your last comment Nick: you say that we should not care about 739 but what if I want to generate a variable "event" that takes place on August 2021? In that case, I need to know what 2021m8 is right? Or is there another way?

            Thanks for the clarification

            Comment


            • #7
              Take a look at -help date function" in general and -help monthly()- and -help mofd()- in particular.
              Code:
              display monthly("2021m8", "YM")  // tells you that it month 739.
              I'm not sure what you mean by "a variable event that takes place on August 2021," since that would be a constant rather than a variable. Perhaps you mean that you want an indicator variable showing whether the date for a particular event happened during August 2021. How to do that would depend on what kind of date variable you have. If you have a daily date, you could do this:
              Code:
              gen byte Aug2021 = inrange(YourDailyDateVariable, date("2021-08-01", "YMD"), date("2021-08-31", "YMD"))
              If you have a monthly date:
              Code:
              gen byte Aug2021 = YourMonthlyDateVariable == monthly("2021m8","YM")
              You could code the preceding using the numeric values of the day or month, but I'd say that such code would be more obscure.

              (I don't frequently use date functions myself, so someone else might suggest nicer approaches.)

              Comment


              • #8
                Mike Lacy gives the best advice here, to look at the documentation for functions.

                I would use ym(2021, 8) myself in this circumstance. Many people use tm(2021m8). The result is the same.

                I suspect that ym() as a function was introduced before tm() so I never switched to using it. But the history of when functions were added to Stata is not important, although it may be interesting to some people.

                Comment

                Working...
                X