Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65161

Extract digits from a number

$
0
0
We have a variable total_response_time formatted in an hour_minute_second way. For example 12301 means 1hour 23 minutes and 01 second, 423 means 4 minutes and 3 seconds, 113456 means 11 hours and 34 minutes and 56 seconds. How can we convert them into seconds? For example 12301 = 1*3600+23*60+1. The tricky part is that some of them have 6 digits, some 5 digits... Therefore, the following codes don't work

gen x = totalresponsetime
tostring x, gen(str_x)
gen xhour=substr(str_x, 1, 2) //pull out the hours(1 means start at the first character and 2 means pull out 4 characters)
gen xminutes=substr(str_x, 3, 2) //pull out the minutes (5 means start at the fifth character and 2 means pull out 2 characters)
gen xseconds = substr(str_x, 5, 2) //pull out the seconds (8 means start at the eight character and 2 means pull out 2 characters)

Viewing all articles
Browse latest Browse all 65161

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>