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

I have code like the following:

 

data a_cal; set Work.a_bgn;
Format TimeBetter Time17.9 ;
TimeBetter=time_mlead1-time_m;
where time_m>0;
run;



theflunkee_0-1638960256129.png

I want to calculate the diff between time_mlead1 and time_m in fractions of a second.
e.g. for row 1: the answer should be 0.00087 seconds, and I don't want it to be rounded to 0 seconds or 1 second.

INTCK seems only work for integer seconds only. 
It is ok to be in any format (date or number), but I want to calculate sum of the difference later.
How should I rewrite my code? Many Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I am not having this problem, so I cannot say why you are having this problem. Formats are irrelevant here.

 

data test;
    time_m='9:30:00.002437't;
    time_m_lead1='9:30:00.002524't;
    difference=time_m_lead1-time_m;
    format time_m time_m_lead1 difference time17.9;
run;

 Capture.PNG

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Just do a data step subtraction. Which you appear to have done.

 

What problems are you having? Doesn't your code work? What is wrong with it?

 

 

--
Paige Miller
theflunkee
Fluorite | Level 6

the above code would yield me 0:00:00.000000000.

I have also converted the time_m and time_mlead1's format to be time17.9....don't know why it wouldn't work

PaigeMiller
Diamond | Level 26

I am not having this problem, so I cannot say why you are having this problem. Formats are irrelevant here.

 

data test;
    time_m='9:30:00.002437't;
    time_m_lead1='9:30:00.002524't;
    difference=time_m_lead1-time_m;
    format time_m time_m_lead1 difference time17.9;
run;

 Capture.PNG

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @theflunkee,

 

I would apply the ROUND function to the difference so as to obtain a clean result:

TimeBetter=round(time_mlead1-time_m, 1e-9);

 

Small differences between relatively large numbers are particularly prone to (relative) rounding errors due to numeric representation issues. Let's take the second record of your sample data as an example, using Windows SAS 9.4M5:

301  data _null_;
302  time_mlead1='09:30:00.002551't;
303  time_m='09:30:00.002524't;
304  TimeBetter=round(time_mlead1-time_m, 1e-9);
305  TimeBroken=time_mlead1-time_m;
306  put (TimeB:)(=best16. /);
307  run;

TimeBetter=0.000027
TimeBroken=0.00002699999459

Even with the standard format BEST16. the rounding error in TimeBroken becomes obvious and of course the condition TimeBroken=0.000027 does not hold, whereas TimeBetter=0.000027 is true.

 

The relative error in TimeBroken is approx. 2.0E-5 percent, which is more than five billion times larger than the unavoidable relative numeric representation error in TimeBetter (using the ROUND function) of approx. 3.7E-15 percent.

 

None of your 20 example records, except the two trivial cases with time_mlead1=time_m, would produce a perfect result without cleaning the rounding error.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 588 views
  • 2 likes
  • 4 in conversation