Announcement

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

  • Difficulty working with time variable

    Hi,

    I thought I had cracked this but seemingly not:

    After having trouble importing variables containing dates from an SPSS file I used -usespss- which looked to have done the trick and after formatting the variables using:

    format time1 %tcHH:MM

    so times are displayed (11.05, 11.15 etc) and ultimately I was looking for a duration of an appointment which I was able to calculate as time2 - time 1 which gave a third var time3 which I formatted as %tcMM so it shows a time in minutes, in this case 10 minutes.

    My problem is that I want this value to behave as a 'normal' number, so I want to multiply var3, as '10' by another variable but I get really strange numbers. I've been reading the 'datetime translation' help files but can't work what I need to do. Is there a way to use/ convert this variable?

    Any thoughts much appreciated.

    Matt



  • #2
    You'll have to post an example of your data and what you've tried using dataex. See post #3 here as an example:

    https://www.statalist.org/forums/for...e-in-two-times

    Comment


    • #3


      I agree with Justin Blasongame that we really need to see a data example.

      Either way, clock times in Stata have units of milliseconds.The fact that you times displayed as hours and minutes does not affect. Your data are not thereby converted to units of minutes (or hours, for that matter).

      If your times are just hours and minutes, I would myself be tempted just to use two versions, a string version and a numeric version in minutes. I would not use Stata datetimes without extra reason.

      Code:
      clear
      input str5 (start end) 
      "11:22" "22:33"
      "08:00" "09:15" 
      end 
      
      gen stime = 60 * real(substr(start, 1, 2)) + real(substr(start, 4, 2)) 
      gen etime = 60 * real(substr(end, 1, 2)) + real(substr(end, 4, 2)) 
      
      gen duration = etime - stime
      
      list 
      
           +------------------------------------------+
           | start     end   stime   etime   duration |
           |------------------------------------------|
        1. | 11:22   22:33     682    1353        671 |
        2. | 08:00   09:15     480     555         75 |
           +------------------------------------------+
      An extra reason, however, might well be periods spanning two or more days.

      Comment


      • #4
        Thank you both for your responses. I was about to post an example though in the meantime a colleague directed me to:

        https://www.stata.com/statalist/arch.../msg01148.html

        where one of the posters describes:
        gen minute = mm(time) which neatly did what I was trying to do.

        Comment


        • #5
          In #3 sorry about a mangled sentence

          The fact that you times displayed as hours and minutes does not affect
          which should have been

          The fact that you have times displayed as hours and minutes does not affect that principle.

          Comment

          Working...
          X