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;
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.
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;
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?
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
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;
Your code is OK, just use a proper format for the result, like 10.6
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.