DATA Step, Macro, Functions and more

Convert time format to integer value

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

Convert time format to integer value

Hello,

I need some help in converting time to numeric value, well not numeric value but convert 00:10:00 to 10 or 00:10:12 to 10.12 in SAS.

I tried using substr first to get only 10:00 but it didn't work.

 

Data dummy;

set test;

time=substr(time1, 3,5); /* To get only 10:00, starting from 3rd digit and get five digits*/

run;

 

I don't want to convert 00:10:00 as 600 which is the numeric value for 10 minutes. I want to take out the colons and just get the number.

 

Any help!!

 

Thank you

M


Accepted Solutions
Solution
a month ago
Super User
Posts: 6,901

Re: Convert time format to integer value

Posted in reply to Malathi13

If you truly have a time value to begin (and not a character string), the accurate conversion formula would be:

 

minutes = timevalue / 60;

 

If you have a time value of 612 that is formatted as 10:12, and you want a not-so-accurate conversion formula to something like 10.12, you can use:

 

minutes = int(timevalue/60) + 0.01 * mod(timevalue, 60);

 

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,251

Re: Convert time format to integer value

[ Edited ]
Posted in reply to Malathi13

I think you have confused the format of a variable and the value of a variable, these are not the same. Even though you see 00:10:12, the underlying value is in seconds. If you want to change 00:10:12 to 10.12, I don't think you did the math properly, the underlying value is 60*10+12 (60 seconds times 10 minutes, plus another 12 seconds). If you want this in minutes, you divide 60*10+12 by 60, and that is not 10.12.

 

I want to take out the colons and just get the number.

 

Remove the format.

 

format time;

Then you see the value 612. Which was the value all along, the format only changed the way it appeared, it did not change the value itself.

--
Paige Miller
Frequent Contributor
Posts: 78

Re: Convert time format to integer value

Posted in reply to PaigeMiller

Hi PaigeMiller,

I think you are right, if I remove the format, I get it in numbers but I want to see if I can just replace colon with a period and get the value as it is. Instead of 10:12 replace colon with period. Maybe using 'compress' function I can do that, not sure.

Respected Advisor
Posts: 3,251

Re: Convert time format to integer value

Posted in reply to Malathi13

@Malathi13 wrote:

Hi PaigeMiller,

I think you are right, if I remove the format, I get it in numbers but I want to see if I can just replace colon with a period and get the value as it is. Instead of 10:12 replace colon with period. Maybe using 'compress' function I can do that, not sure.


You cannot replace the colon with a period. You are still confusing the format with the value. The value is 612. You could create a mathematical formula in SAS to convert 612 to 10.12, I will leave that up to you to figure out how to do that.

 

 

--
Paige Miller
Super User
Posts: 23,951

Re: Convert time format to integer value

Posted in reply to Malathi13

Convert to a character using PUT()

Use COMPRESS() to remove colon

Use INPUT() to read back in as integer.

Solution
a month ago
Super User
Posts: 6,901

Re: Convert time format to integer value

Posted in reply to Malathi13

If you truly have a time value to begin (and not a character string), the accurate conversion formula would be:

 

minutes = timevalue / 60;

 

If you have a time value of 612 that is formatted as 10:12, and you want a not-so-accurate conversion formula to something like 10.12, you can use:

 

minutes = int(timevalue/60) + 0.01 * mod(timevalue, 60);

 

 

Frequent Contributor
Posts: 78

Re: Convert time format to integer value

Posted in reply to Astounding

Hi Astounding,

Thank you so so much!! Perfect conversion, I got 10:12 as 10.12

 

M

Respected Advisor
Posts: 3,251

Re: Convert time format to integer value

Posted in reply to Malathi13

@Malathi13 wrote:


Thank you so so much!! Perfect conversion, I got 10:12 as 10.12

 


And don't try to do any math with this value 10.12, because although it's a number, it's not really a number that can be added or subtracted, etc

--
Paige Miller
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 73 views
  • 2 likes
  • 4 in conversation