Announcement

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

  • Change value of a date variable

    Dear all

    I probably have a very simple question, yet I cannot solve it.

    I have a variable called "Zeitpunkt" which is in double type and %tCMonth_dd,_CCYY_HH:MM format.
    One observation has following value: January 19, 2022 22:00
    I now want to change that to January 19, 2023 22:00

    Unfortunately, stata always tells me that there is a type missmatch.
    Any ideas how to change the value?

    Thanks a lot!
    Antonia

  • #2
    We can't see the code you used to try to replace, but I guess you tried to change what is a value of a numeric variable by specifying a different string value -- and that is indeed a type mismatch. A type mismatch occurs when you offer a numeric expression when only a string expression will do, or conversely.

    It's the same date and time except for being one year later, so 365 * 24 * 60 * 60000 milliseconds later, or greater.



    Code:
    . di %tc clock("January 19, 2022 22:00", "MDY hm")
    19jan2022 22:00:00
    
    . di %tc clock("January 19, 2022 22:00", "MDY hm") + 365 * 24 * 60 * 60000
    19jan2023 22:00:00
    shows that in principle that way of doing things is correct. I think it would often be considered bad coding style, so anyone is more than welcome to suggest better ways to do it.

    Comment


    • #3
      Well, for people who are very familiar with datetime functions, the following code is a more transparent way to do it, in the technical sense of being a direct restatement in code of the purpose of the command. But as most of the datetime functions in this command are little used, to many readers I would imagine this approach is vastly more opaque! (Another limitation of this code is that it requires version 17 or later, as the -birthday()- function was not introduced until then.)
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double zeitpunkt
      1.9582488e+12
      end
      format %tCMonth_dd,_CCYY_HH:MM zeitpunkt
      
      gen double corrected_zeitpunkt = ///
          cofC(Cdhms(birthday(dofc(zeitpunkt), 2023), hh(zeitpunkt), mm(zeitpunkt), ss(zeitpunkt)))
      format corrected_zeitpunkt %tCMonth_dd,_CCYY_HH:MM zeitpunkt
      
      list, noobs clean

      Comment


      • #4
        I didn't spot that Antonia is using %tC not %tc. How much difference leap seconds make if your data have the granularity of minutes can be explored at leisure.

        Other way round, why cofC() here?

        Comment


        • #5
          Using -cofC()- was a mistake. I normally work with clock, not Clock, variables, so I got a little confused when writing this code.

          Given that she is using tC, and with my own confusion about c vs C resolved, my code needs to be modified to:
          Code:
          gen double corrected_zeitpunkt = ///
              Cdhms(birthday(dofC(zeitpunkt), 2023), hh(cofC(zeitpunkt)), ///
              mm(cofC(zeitpunkt)), ss(cofC(zeitpunkt)))


          Comment

          Working...
          X