Announcement

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

  • Datepart/Clockpart

    Hi everyone
    In my dataset I have a time variable. It is in format: %tcDay_Mon_DD_HH:MM:SS_CCYY (this is variable “Date”). Storage type float.
    According to the ddatetime.pdf I should be able to extract for example year from that variable.
    I have tried both:
    gen RegYear = clockpart(Date, “year”) and
    gen RegYear = datepart(Date, "year")
    But both generates an error message (for clockpart): unknown function clockpart() r(133);
    What am I doing wrong?
    All the best!

  • #2
    Have you tried this?
    Code:
    gen RegYear = year(Date)

    Comment


    • #3
      Thanks for your suggestion. I have tried that but it only generated missing values…

      Comment


      • #4
        If you have a date-time variable it should be double not float as advised prominently within help datetime

        Otherwise I suspect you're reading a help file that is more up-to-date than your Stata. Please let us know if your version is <17 (https://www.statalist.org/forums/help#version).

        Ken Chui #2 year() pulls the calendar year out of a daily date but for a date-time variable you need year(dofc())

        Comment


        • #5
          What version of Stata are you using? The clockpart() function was added in Stata 17.0; if you are using an earlier version it does not exist, and would not be mentioned in the output of help datetime.

          The year function mentioned in post #2 only works on SIF date values, so perhaps
          Code:
          gen RegYear = year(dofc(Date))
          will do what you seek.

          You have a second problem. You tell us your datetime value is storage type float. The output of help datetime tells us that when you convert a string to a Stata Internal Format datetime numeric value, you must create the numeric value as double, not float. Having created your variable Date as float, it is imprecise and this cannot be corrected by converting it from float to double. You must return to your original string and convert it using
          Code:
          generate double Date = clock( ...
          Last edited by William Lisowski; 05 Nov 2021, 07:19.

          Comment


          • #6
            Thank you so much! This explain why I could not make it work. I have STATA 16… But
            gen RegYear = year(dofc(Date)) worked out fine. It also worked out fine to extract weekday using “dow” instead of “year”.
            Thanks again!

            Comment


            • #7
              Worked out fine?

              Yes, in that year() and dofc() are supported in Stata 16, and for some versions previously.

              No, in that as William Lisowski and I flagged, a datetime variable that is float was probably messed up in detail, so checking results very carefully could be in order,

              Comment

              Working...
              X