BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Asquared
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.


 

View solution in original post

3 REPLIES 3
Reeza
Super User

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.


 

PaigeMiller
Diamond | Level 26

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. 

--
Paige Miller
novinosrin
Tourmaline | Level 20

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

 

sas-innovate-white.png

Special offer for SAS Communities members

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.

 

View the full agenda.

Register now!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1519 views
  • 0 likes
  • 4 in conversation