DATA Step, Macro, Functions and more

Time output values vary when put function is used

Reply
Super Contributor
Posts: 272

Time output values vary when put function is used

Dear,

 

In my data the following values present for a time variable.

 

When I used put function to convert to character, I am getting different values eventhough the time values are same. Any suggestions. Please help. Thanks

 

 code; Time1=put(time,time8.);

 

 

Data

 

ID           Time(numeric)

1            00:00:00

1            00:00:00

 

output getting;

ID             Time                        time1

1            00:00:00                    0:00:00

1            00:00:00                    24:00:00

 

Super User
Posts: 19,876

Re: Time output values vary when put function is used

Posted in reply to knveraraju91

Are there no other components to the date, such as a decimal or a day value?

 

Most likely this is a numerical precision issue. If you round it to the nearest integer and format it should work as expected.

 

Try

 

time_char = put(round(time, 1), time8.);

FYI - I could not replicate your issue. 

Super User
Posts: 7,866

Re: Time output values vary when put function is used

Posted in reply to knveraraju91

Assign a format of best32. to your time variable, and then look at the values; you will find that there is a small fractional aberration.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,866

Re: Time output values vary when put function is used

Posted in reply to knveraraju91

And once you've verified that you have a "numerical precision artifact" in your time value, just use the round() function to get rid of it.

timeval = round(timeval,.000001);

will remove a small fractional error while preserving time down to microseconds.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 131 views
  • 3 likes
  • 3 in conversation