Announcement

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

  • String to time variable problem (missing values)

    Hi,

    I have string (type str8 and format %9s) variable nammed pickup_time
    wich has this format ;

    1:12:00
    5:34:00
    and so on ..

    I am trying to put that in time format variable; i tried these 2 commands;


    gen double pickup_hrs = clock( pickup_time , "hms")
    gen pickup_hrs = clock( pickup_time , "h:m:s")

    the command works but nothing shows up, i keet getting "179,759 missing values generated"
    There's no date, only the time, i could also remove the last part xx:xx:00 since it's always zero (no seconds)

    What is my error here?
    Thank you

  • #2
    Your first command looks correct. Just need to divide it by 3600000 to get hours, since it will give you miliseconds.

    Comment


    • #3
      Thank you for the quick answer,
      How to tell Stata to divide by that number? Will it be at the end of the command?

      I tried ;

      gen pickup_hrs1 = pickup_hrs/3600000

      But the output is numbers, no time format

      edit : I managed to get partly what i want with the command ; format pickup_hrs %tc
      But now the variables has also 01jan1960 for every observation, is there a way to remove that and keep the time only?


      pickup_hrs
      01jan1960 01:21:00
      01jan1960 06:20:00
      01jan1960 01:36:00
      01jan1960 04:30:00
      01jan1960 05:45:00
      01jan1960 02:44:00
      01jan1960 04:58:00
      01jan1960 04:09:00
      01jan1960 12:38:00
      01jan1960 10:19:00
      01jan1960 12:50:00
      01jan1960 12:27:00
      01jan1960 02:42:00
      01jan1960 03:44:00
      01jan1960 12:09:00

      Last edited by Rabah Kechad; 21 Oct 2015, 16:21.

      Comment


      • #4
        Don't reformat. Rethink. After division, it's giving you hours since midnight, which seemed to be what you wanted. Or maybe that's not what you wanted?

        Comment


        • #5
          The output of help datetime display formats will explain how to construct a more detailed format to suppress the (meaningless) date portion of a datetime value in your display of the pickup time. As Ben suggests, though, the display of the time doesn't relate to how it is stored for calculations by Stata.

          If you have not already done so, you will want to read the guidance in help datetime, which is without a doubt the most visited documentation on my system, with the second-most-visited being Chapter 24 (Working with dates and times) of the Stata User's Guide PDF available from the PDF Documentation item on Stata's Help menu. Before working with dates and times, any Stata user should read the very detailed Chapter 24 thoroughly. After that, the help documentation will usually be enough to point the way. Some people may be able to remember everything without have to continually refer to the documentation, but I for one am not such a person.
          Last edited by William Lisowski; 21 Oct 2015, 20:15.

          Comment


          • #6
            Thank you for you answers,
            ​
            What is the difference by working with a format %tc like this ;

            pickup_hrs
            01jan1960 09:35:00


            Or, like Ben told me to do and work with hours like this;

            pick_heure
            9.583333

            (William, i am still reading the documentation to remove the data portion)

            Comment


            • #7
              It is the difference between a date (or time) on the one hand and a duration.

              Consider this:

              1, We had a long lunch discussing Stata. In fact it lasted 2 hr 30 min. That's a duration. It is not fixed to any time scale. There is no information about precisely when it was.

              2. Meet for lunch on Thur 5 Nov 2015 at 14:30. That's a date (plus time). Enjoy the date.

              It's not clear which you have. I will guess you have durations. If you have durations like 1:12:00 in hours, minutes, seconds, then the convenient units of measurement could be any of hours, minutes, seconds. We can't tell which is best for you, and for some purposes any or all of those might make sense.

              What is most unlikely if you have durations is that a notional origin of 1 January 1960 00:00:00 makes any sense. The default %tc format doesn't help. In fact you won't really need any date or time display format if you have units you understand.

              Similarly, what is unlikely if you have durations as reported in your examples is that units of milliseconds help anything.

              But you need to convert strings in the form hh:mm:ss to something quantitative.

              I often feel my way with dates and times using display as a calculator and simple examples where I can see what is happening.

              Code:
              . di clock("1:12:00", "hms")
              4320000
              
              . di clock("1:12:00", "hms")/ (60 * 1000)
              72
              So, the clock() function will give a duration in milliseconds. Dividing by 60,000 gives me a duration in minutes. That sounds fine to me, but you need to make your own decision.

              Comment


              • #8


                The strings variable is in hh:mm:ss format, it's the hour, a moment (i have another variable for duration already)
                Ex : i want to see an effect at certain period of the day let say an effect on price if we're past 6pm or before noon.

                I can do this with the hours format i guess?

                I also have separate variables for dates and months too
                Last edited by Rabah Kechad; 27 Oct 2015, 13:14.

                Comment


                • #9
                  If you just care about time of day, then you may also need to think about it on a circular scale. Depends if your data wrap around midnight, or are just middle of the day.

                  Comment


                  • #10
                    The hours goes from 1 to 12, i have a binary variable AM = 1, 0 if PM

                    so 00h06 would be 12.1 and Variable AM = 1

                    Comment


                    • #11
                      So your conversion needs to take account of that. PM times: add 12 hours.

                      Comment


                      • #12
                        Thank you Nick, i'll work on it

                        Comment


                        • #13
                          Hi, I seem to have a similar problem to Rabah.

                          I have 2 string variables admtimedate (str14) and distimedate (str14)

                          "list admtimedate" and "list distimedate" would give:
                          1/1/16 1:08
                          11/1/16 14:10
                          dd/mm/yy hh:mm
                          ...

                          I would like to find the time duration between distimedate and admtimedate, expressed in hours.

                          I have tried using the clock function to transform the variable from string into elapsed date/time, as below:
                          CODE:
                          gen double admpoint = clock(admdatetime, "DMYhm")
                          gen double dispoint = clock(disdatetime, "DMYhm")
                          format admpoint dispoint %tc
                          but each generate command gives me: "22000 missing values generated" (essentially the same number of observations per variable I have)

                          if I can get the dates in numerical format, i can do the simple arithmetic to derive my duration in hours, but what seems to be the issue is the initial conversion from string to numeric. would appreciate your advice in this matter.

                          thank you!

                          Comment


                          • #14
                            oh i figured it out:
                            gen double admpoint = clock(admdatetime, "DM20Yhm")
                            gen double dispoint = clock(disdatetime, "DM20Yhm")
                            format admpoint dispoint %tc

                            thanks anyway!

                            Comment

                            Working...
                            X