Obsidian | Level 7

## How do I convert character date time to numeric time?

I have a field contains time like this 1/1/1970 6:53:58.000000 PM, how to convert to 18:53:58? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How do I convert character date time to numeric time?

Hi @novinosrin,

Yes, I should have commented on this. The ANYDTTME30. informat would read the decimals in any case. However, I suspect that internally a SAS datetime value is created in the first place, from which the time value is then extracted. The algorithm doing the extraction suffers from numeric representation issues. Note that current SAS datetime values are >1E9, so that a true precision of 6 decimals (i.e. 10+6>15 decimal digits in total) cannot be achieved in general. I remember a discussion about TIMEPART in this forum earlier this year (?) where this was the issue.

By using the ROUND function I remove the artifacts introduced by the algorithm.

Example:

``````data have;
dtc='1/1/1970 6:53:58.654321 PM';
run;

data _null_;
set have;
t=round(input(dtc, anydttme30.), 1e-6);
put (t:) (=best18.);
run;``````

Result:

`t_bad=68038.6543210148 t=68038.654321`

Actually, if the six decimals are non-zero (for some values) and they are very important, one should prefer an approach extracting the "time part" first as a substring and then apply a time informat to that substring. This would avoid the somewhat risky internal computations involving numbers with possibly insufficient precision.

7 REPLIES 7
Amethyst | Level 16

## Re: How do I convert character date time to numeric time?

you could try anydtdte.

``````data want;
set have;
date=timepart(input(date,anydtdte.));
format time time8.;
run;``````
Thanks,
Jag

## Re: How do I convert character date time to numeric time?

Hi @grace999,

Are the decimals always zero? If so, you can use

``t=input(dtc, anydttme30.);``

for the transformation, where dtc is the name of the date time (character) variable. The result is a SAS time value and can be formatted with the TIME8. format to obtain the desired output.

Example:

``````data have;
dtc='1/1/1970 6:53:58.000000 PM';
run;

data want;
set have;
t=input(dtc, anydttme30.);
format t time8.;
run;``````

If some values have non-zero decimals and you want to preserve these (internally), use

``t=round(input(dtc, anydttme30.), 1e-6);``

Tourmaline | Level 20

## Re: How do I convert character date time to numeric time?

Mr Genius, May i request a couple of comments on how this

``t=round(input(dtc, anydttme30.), 1e-6);``

round 1e-6 works plz?

i.e when you have time at your own convenience. Thank you!

## Re: How do I convert character date time to numeric time?

Hi @novinosrin,

Yes, I should have commented on this. The ANYDTTME30. informat would read the decimals in any case. However, I suspect that internally a SAS datetime value is created in the first place, from which the time value is then extracted. The algorithm doing the extraction suffers from numeric representation issues. Note that current SAS datetime values are >1E9, so that a true precision of 6 decimals (i.e. 10+6>15 decimal digits in total) cannot be achieved in general. I remember a discussion about TIMEPART in this forum earlier this year (?) where this was the issue.

By using the ROUND function I remove the artifacts introduced by the algorithm.

Example:

``````data have;
dtc='1/1/1970 6:53:58.654321 PM';
run;

data _null_;
set have;
t=round(input(dtc, anydttme30.), 1e-6);
put (t:) (=best18.);
run;``````

Result:

`t_bad=68038.6543210148 t=68038.654321`

Actually, if the six decimals are non-zero (for some values) and they are very important, one should prefer an approach extracting the "time part" first as a substring and then apply a time informat to that substring. This would avoid the somewhat risky internal computations involving numbers with possibly insufficient precision.

Tourmaline | Level 20

## Re: How do I convert character date time to numeric time?

Slick and can't be more neat. Thank you for your time, just added to my notes.

## Re: How do I convert character date time to numeric time?

@FreelanceReinh wrote:

Actually, if the six decimals are non-zero (for some values) and they are very important, one should prefer an approach extracting the "time part" first as a substring and then apply a time informat to that substring. This would avoid the somewhat risky internal computations involving numbers with possibly insufficient precision.

Interesting: It has turned out that time informats are prone to numerical accuracy issues as well.

Example:

``````data _null_;
t=input('12:12:17.932445 AM',time18.);
put t= best18.;
run;``````

Result:

`t=737.932444999998`

So, their results would require rounding, too.

In a check of 50 million random date time values (with six decimals) from the date range 1970 - 2050 the formula suggested earlier (t=round(input(dtc, anydttme30.), 1e-6);)  produced only correct results.

Obsidian | Level 7

## Re: How do I convert character date time to numeric time?

Thank you so much for your help!

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