- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a field contains time like this 1/1/1970 6:53:58.000000 PM, how to convert to 18:53:58? Thanks.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
you could try anydtdte.
data want;
set have;
date=timepart(input(date,anydtdte.));
format time time8.;
run;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Slick and can't be more neat. Thank you for your time, just added to my notes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much for your help!