Dear stata community,
I am using STATA 18.
I have 20 time variables in HH:MM: SS format with no date. Except for 6, others are strings.
I want to calculate the duration by taking the difference between stop and start times. So, I need to format those variables as time/numeric to take the time duration. However, due to the type mismatch, I cannot do that. I have tried the clock function, but the result produces a time format as YMDhms, which is wrong because I have only hms.
*****The time command I am using-
generate double starttime_1=clock(starttime1,"hms") \\ Gives wrong milliseconds
format starttime_1 %tc
***My goal is the following-
forvalues x = 1/16 {
gen duration_`x'= (stoptime`x' - starttime`x')/60000
}
Please guide me to make the string variables as time/numeric variables?
Thanks.
I am using STATA 18.
I have 20 time variables in HH:MM: SS format with no date. Except for 6, others are strings.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str8 wakeup str11(start end) str8(starttime1 stoptime1 starttime2 stoptime2) "" "11:15:19 AM" "1:03:12 PM" "" "" "" "" "7:00:00" "2:29:37 PM" "3:39:30 PM" "7:00:00" "21:00:00" "" "" "7:05:00" "4:55:34 PM" "7:37:57 PM" "10:00:00" "18:05:00" "" "" "6:00:00" "2:41:35 PM" "9:50:39 PM" "6:45:00" "9:30:00" "9:00:00" "10:00:00" "" "11:49:16 AM" "1:49:32 PM" "" "" "" "" "" "12:55:35 PM" "1:12:24 PM" "" "" "" "" "" "5:08:28 PM" "5:17:44 PM" "" "" "" "" "" "2:59:45 PM" "3:27:39 PM" "" "" "" "" "8:00:00" "1:53:49 PM" "5:32:27 PM" "9:00:00" "17:00:00" "" "" end
I want to calculate the duration by taking the difference between stop and start times. So, I need to format those variables as time/numeric to take the time duration. However, due to the type mismatch, I cannot do that. I have tried the clock function, but the result produces a time format as YMDhms, which is wrong because I have only hms.
*****The time command I am using-
generate double starttime_1=clock(starttime1,"hms") \\ Gives wrong milliseconds
format starttime_1 %tc
***My goal is the following-
forvalues x = 1/16 {
gen duration_`x'= (stoptime`x' - starttime`x')/60000
}
Please guide me to make the string variables as time/numeric variables?
Thanks.
Comment