BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

In my data set there are two time fields.

With my eyes I see same time in both fields.

I calculate difference between them (difference time in seconds) and I expect to get 0 but I get positive number

proc sql;
create table a as
select  Matefet_Offer_Time,application_Time,Matefet_Offer_Time-application_Time as dif
from ttt_A1e
where Agreement_Account_Id=649012587
;
quit;
 

Ronein_0-1758473274484.png

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You have assigned formats to these variables that do not show fractions of a second. If your format did show fractions of a second, then you should see the problem.

 

--
Paige Miller
Tom
Super User Tom
Super User

If you only want the difference in seconds then round or truncate the values to integers.

You might want to think about whether you want to round before the subtraction or round afterwards as the order can effect the results. 

1    data test;
2      t1='22:09:43't + 0.22 ;
3      t2='22:09:43't - 0.22 ;
4      format t1 t2 tod8.;
5      diff = t1-t2 ;
6      put t1= t2= diff=;
7      put t1=time12.3 t2=time12.3 diff= ;
8      diff = round(t1-t2,1);
9      put t1=time12.3 t2=time12.3 diff= ;
10     diff = round(t1,1) - round(t2,1);
11     put t1=time12.3 t2=time12.3 diff= ;
12     diff = int(t1) - int(t2);
13     put t1=time12.3 t2=time12.3 diff= ;
14     diff = int(t1-t2);
15     put t1=time12.3 t2=time12.3 diff= ;
16   run;

t1=22:09:43 t2=22:09:43 diff=0.44
t1=22:09:43.220 t2=22:09:42.780 diff=0.44
t1=22:09:43.220 t2=22:09:42.780 diff=0
t1=22:09:43.220 t2=22:09:42.780 diff=0
t1=22:09:43.220 t2=22:09:42.780 diff=1
t1=22:09:43.220 t2=22:09:42.780 diff=0

 

Ksharp
Super User

As Tom showed you , just keep integer part of time and drop decimal part of time by INT() if you want to compare two TIME or DATETIME variable.

proc sql;
create table a as
select  Matefet_Offer_Time,application_Time,
int( Matefet_Offer_Time ) - int( application_Time ) as dif
from ttt_A1e
where Agreement_Account_Id=649012587
;
quit;
Kurt_Bremser
Super User

Always keep in mind that in SAS, time and datetime values are counts of seconds. A fractional difference must therefore come from fractions of seconds, which are not displayed by the formats assigned to your variables.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 360 views
  • 0 likes
  • 5 in conversation