Announcement

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

  • Converting a simple string time variable into minutes

    Hi
    I am a beginner at date and time variables in Stata. I understand the principles of date and time variables in Stata but have run into a problem to convert a really simple time variable to minutes. The time variable is a string variable representing the difference between two time points (time of treatment - time of symptom) and include only information about hours and minutes like this example below:
    time_dif
    00:43
    07:28
    13:24
    04:45

    I have read a lot of good instructions to handle time and date variables in Stata but not for a situation with a simple time variable representing a time difference between two time points when no information is available regarding the two different time points (and the date). I suspect that there are a simple solution for this problem but I would really appreciate if someone could give me some advice.

  • #2
    Here as quite often a direct approach using what you knew before age 11 will work fine without record to Stata's date-time functions.

    Indeed, if values such as "25:00" are possible this approach is better.

    Here are two ways to do it:

    Code:
    clear
    input str5 time_dif
    "00:43"
    "07:28"
    "13:24"
    "04:45"
    end 
    
    gen wanted1 = 60 * real(substr(time_dif, 1, 2)) + real(substr(time_dif, -2, 2))
    
    split time_dif, parse(:) destring 
    
    gen wanted2 = 60 * time_dif1 + time_dif2 
    
    list 
    
         +----------------------------------------------------+
         | time_dif   wanted1   time_d~1   time_d~2   wanted2 |
         |----------------------------------------------------|
      1. |    00:43        43          0         43        43 |
      2. |    07:28       448          7         28       448 |
      3. |    13:24       804         13         24       804 |
      4. |    04:45       285          4         45       285 |
         +----------------------------------------------------+
    
    . 
    .

    Comment

    Working...
    X