I'm working on a dataset which has a variable "phonecall_duration". The variable is in Character format and the values in rows look something like this :
phonecall_duration |
0 years 0 mons 0 days 0 hours 0 mins 48.00 secs |
0 years 0 mons 0 days 1 hours 2 mins 15.00 secs |
0 years 0 mons 0 days 2 hours 7 mins 28.00 secs |
0 years 0 mons 0 days 0 hours 9 mins 27.00 secs |
What I want is a variable "minutes" which basically calculates this time duration in minutes only(i.e by adding hours+minutes+sec) with up to 2 decimal places. The table will look like this.
minutes |
0.8 |
62.25 |
127.47 |
9.45 |
I read a lot of documents and codes however I couldn't figure out an efficient way to do this since the variable is not a SAS TIME format. Please suggest.
It looks like a well formatted string, so use SCAN() to get the nth elements in this case the 1st is year, 3 is months, 5 is days etc. Then do the math using the SUM() function. You'll likely need INPUT() to convert it to a number as well to avoid type conversion notes in your log.
If you've 'read a lot of documents and codes' it helps to also show what you've tried so we don't suggest things you already know don't work.
@Asquared wrote:
I'm working on a dataset which has a variable "phonecall_duration". The variable is in Character format and the values in rows look something like this :
phonecall_duration 0 years 0 mons 0 days 0 hours 0 mins 48.00 secs 0 years 0 mons 0 days 1 hours 2 mins 15.00 secs 0 years 0 mons 0 days 2 hours 7 mins 28.00 secs 0 years 0 mons 0 days 0 hours 9 mins 27.00 secs
What I want is a variable "minutes" which basically calculates this time duration in minutes only(i.e by adding hours+minutes+sec) with up to 2 decimal places. The table will look like this.
minutes 0.8 62.25 127.47 9.45
I read a lot of documents and codes however I couldn't figure out an efficient way to do this since the variable is not a SAS TIME format. Please suggest.
It looks like a well formatted string, so use SCAN() to get the nth elements in this case the 1st is year, 3 is months, 5 is days etc. Then do the math using the SUM() function. You'll likely need INPUT() to convert it to a number as well to avoid type conversion notes in your log.
If you've 'read a lot of documents and codes' it helps to also show what you've tried so we don't suggest things you already know don't work.
@Asquared wrote:
I'm working on a dataset which has a variable "phonecall_duration". The variable is in Character format and the values in rows look something like this :
phonecall_duration 0 years 0 mons 0 days 0 hours 0 mins 48.00 secs 0 years 0 mons 0 days 1 hours 2 mins 15.00 secs 0 years 0 mons 0 days 2 hours 7 mins 28.00 secs 0 years 0 mons 0 days 0 hours 9 mins 27.00 secs
What I want is a variable "minutes" which basically calculates this time duration in minutes only(i.e by adding hours+minutes+sec) with up to 2 decimal places. The table will look like this.
minutes 0.8 62.25 127.47 9.45
I read a lot of documents and codes however I couldn't figure out an efficient way to do this since the variable is not a SAS TIME format. Please suggest.
You have to parse the string into the needed parts, using the SCAN function. So the 7th "word" is the number of hours, the 9th "word" is the number of minutes and so on. From there, it ought to be easy to compute the desired number of minutes.
Can't agree more with Reeza's exquisite response. More than any docs, read through char functions, how you would extract the nums you need to compute the sum.
1st extract nums you need( look for functions)
2nd compute the sum after dividing the seconds num by 60
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.