Announcement

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

  • Sleep time calculation

    Dear Stata Users,

    I have just started to use Stata and I am trying to calculate sleep duration (difference) between bedtime and wakeup time.
    Data are in minutes after midnight. I tried different methods but what works for some does not work the others.
    Bed_time Wakeup_time
    1380 360
    60 480

    Thank you in advance
    Fatos.
    Last edited by Fatos Simnica; 08 Jan 2020, 05:59.

  • #2
    because you did not follow the advice in the FAQ (please read it), there is no way to be sure but I believe you have not used Stata's datetime facilities; please read
    Code:
    help datetime
    once the times are in Stata's time format, the calculation should be easy (just subtract)

    Comment


    • #3
      Just this once I disagree with my friend and co-author Rich Goldstein. I see no real gain in switching to units of milliseconds. (He is right, however, if sleep extends over 3 or more days.)

      The examples seem to include the two obvious possibilities: going to bed before and after midnight,

      Code:
      gen wanted = cond(Wakeup_time <= Bed_time, Wakeup_time - Bed_time, 1440 - Bed_time + Wakeup_time)
      as 1440 = 24 * 60.

      Comment


      • #4
        Thank you very much Nick, that helped me solve the problem, but i was geting negative values so i changed it just a bit.

        code;

        gen sleeptime = cond ( bed_time > wakeup_time, wakeup_time + 1440 - bedtime, wakeup_time - bed_time)
        Last edited by Fatos Simnica; 08 Jan 2020, 07:07.

        Comment


        • #5
          Could you provide an example for which the use of Nick's formula was resulting in negative values?

          Comment


          • #6
            The first inequality should be >= (clearly, not enough sleep).

            Comment


            • #7
              Thanks again Nick!

              And Eric, in the same example that i provided before.

              Comment


              • #8
                Nick wrote, "not enough sleep": Probably the same here. I was mixing up true and false, and may be the order of the operators.
                Fatos: as you say the examples are above.
                (edited version)
                Last edited by Eric de Souza; 08 Jan 2020, 07:43.

                Comment


                • #9
                  Mixing up true and false remains a big problem....

                  Comment

                  Working...
                  X