Announcement

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

  • Merging time variables into one variable

    Hello everyone,

    I have a dataset with variables that describe the time spent on different activities: e.g. activity 1 starts at 7:15am and finishes at 9:00am. The trouble is, the hours and the minutes of each start and end time are listed in separate columns: so the 'hours' are in one column and the 'minutes' are in the next column. My end goal is to create a variable that describes the duration spent on each activity but I am guessing that I first need to create a variable that combines hours and minutes in one column? I am not having much luck searching youtube and stata's help function - can anyone please help me out with the syntax for creating these variables?

    Thanks in advance!





  • #2
    Hi Alexander, welcome to statalist

    I suppose you could generate a new variable in which you add the two different variables together (the minutes and the hours) simply use;
    "gen total_time= whatever your hour variable is called + what ever your minute variable is called" .
    Failing this you could try generate the difference in hours (i.e. end hour - start hour) and difference in minutes and then combine this to get the total effect. If you install dataex and provide a sample of your data I can be of more use to you. You may need to convent the time into minutes as I do not know of a way of getting Stata to understand that 1:45 is 1HR 45M and not something else. Although some of my fellow Statalist users may be able to help you there.

    Kind regards,
    Jordan

    Comment


    • #3
      As Jordan suggested, a sample of your data would be useful. I'm going to assume that your data are numeric, and however you indicate AM/PM, I'll ignore that and assume that none of the elapsed times are more than 12 hours. With that said, here is some sample code that may start you in a useful direction.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(hst mst hend mend)
       7 15 9  0
      12 50 1 10
      end
      generate double tst = hms(hst,mst,0)
      generate double tend = hms(hend,mend,0)
      generate elapse = minutes(tend-tst)
      replace elapse = elapse + 12*60 if tend<tst
      format %tcHH:MM tst tend
      list, clean noobs
      Code:
      . list, clean noobs
      
          hst   mst   hend   mend     tst    tend   elapse  
            7    15      9      0   07:15   09:00      105  
           12    50      1     10   12:50   01:10       20
      With that said, some advice to make your task easier.

      Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

      All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Comment


      • #4
        Thank you both so much for your replies - the syntax you have provided works as you correctly guessed how my data looks, which is great! I will definitely be re-reading the chapter. Much appreciated!

        Comment

        Working...
        X