turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Convert time format to integer value

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

a month ago

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago

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);

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago - last edited a month ago

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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

a month ago

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago

@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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago

Convert to a character using PUT()

Use COMPRESS() to remove colon

Use INPUT() to read back in as integer.

Solution

a month ago

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago

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);

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

a month ago

Hi Astounding,

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

M

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Malathi13

a month ago

@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

Paige Miller