Announcement

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

  • date variables: some hours written as hh:mm:59 and not rounded

    Hi everyone,

    I have a very easy question for stata specialists. I have some of my date's hours that are not in the "format" that I want


    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(date_hour price)
    1.9407168e+12 91.99
    1940720399999.9998 91.26
    1940723999999.9995 91.65
    1.9407276e+12 91.99
    1940731199999.9998 91.99
    end
    format %tc date_hour
    [/CODE]

    For example, the value above in red is written as 01jul2021 00:59:59, instead of 01:00:00.
    Basically, my hours should be instead 00:00:00, 01:00:00, 02:00:00, and so on.

    Could anyone please give me the solution to that?
    Thanks.

    Michael

  • #2
    An answer is that date-times are numeric with units ms. Therefore if (and only if) you know all times should be on the hour, push them through round() with second argument 60 * 60 * 1000.

    Comment


    • #3
      Good afternoon Nick Cox,

      Thank you so much for your help, again. In my case, yes, all times should be on the hour.

      However, I have a few questions to ask you please:
      1. Sorry, what do you mean by second argument?
      2. Why are you doing 60*60*1000?
      Sorry, I am not proficient yet in datetime variables management.
      Thank you in advance for your answer.
      Best,

      Michael
      Last edited by Michael Duarte Goncalves; 02 Nov 2023, 10:28.

      Comment


      • #4
        In a hour there are 60 minutes. In a minute there are 60 seconds. In a second there are 1000 milliseconds.

        In a function call such as function(foo, bar) foo is the first argument and bar is the second argument. https://en.wikipedia.org/wiki/Argument_of_a_function

        Comment


        • #5
          Ok, thank you for your time! All is clear now.

          All the best Nick,

          Michael
          Last edited by Michael Duarte Goncalves; 02 Nov 2023, 10:30.

          Comment


          • #6
            Hi again,

            Unfortunately, I still obtain some weird results:

            Code:
            gen test = round(date_hour, 60*60*1000)
            dataex:


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float test
            1.9407152e+12
            1.9407187e+12
            1.9407224e+12
             1.940726e+12
            1.9407296e+12
            end
            format %tc test
            Should that be better?

            Code:
            gen double time = round(clock(date_hour, "MDY hms"), 60*60*1000)
            format %tc time
            Edit: Oh, forget about the code above, date_hour should be a string for the code above to work properly, right?

            Thank you again for your help.
            Best,

            Michael
            Last edited by Michael Duarte Goncalves; 02 Nov 2023, 10:48.

            Comment


            • #7
              The first argument of clock() must indeed be a string. Otherwise the idea works for me:

              Code:
              . di %tc  round(1940720399999.9998, 60 * 60 * 1000)
              01jul2021 01:00:00

              Comment


              • #8
                I'm not sure what is being said in #6. The key problem is that -gen test = ...- creates test as a float. A float does not have enough bits to store a Stata date time variable without losing precision. It must be -gen double test = ...-. There seems to be some recognition of this in what follows later in that post, but I'm not sure, so I just want to drive that point home. Date time variables in Stata must always be created as -double-s, and doing that requires explicit mention of that in the -gen- statement, because the default is the inadequate (for this purpose) float.

                Comment


                • #9
                  Hi Nick Cox,
                  Hi Clyde Schechter,

                  Thank you for your help in #7 and #8.
                  • #7: It works perfectly as you told me. Thank you!
                  • #8: You are right Clyde, I forgot to put the -gen double-. So the code written in #6 should be rather:
                  Code:
                  gen double test = round(date_hour, 60*60*1000)
                  You said that to me in one of my previous posts. I'm sorry, I completely overlooked this specificity.
                  Many thanks to both of you for your help.
                  Have a nice morning/day/evening.

                  All the best,

                  Michael

                  Comment

                  Working...
                  X