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

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
FreelanceReinh
Jade | Level 19

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_bad=input(dtc, anydttme30.);
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.

View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

you could try anydtdte.

 

data want;
set have;
date=timepart(input(date,anydtdte.));
format time time8.;
run;
Thanks,
Jag
FreelanceReinh
Jade | Level 19

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

 

 

novinosrin
Tourmaline | Level 20

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! 

FreelanceReinh
Jade | Level 19

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_bad=input(dtc, anydttme30.);
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.

novinosrin
Tourmaline | Level 20

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

FreelanceReinh
Jade | Level 19

@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.

grace999
Obsidian | Level 7

Thank you so much for your help!

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 5463 views
  • 2 likes
  • 4 in conversation