BookmarkSubscribeRSS Feed
vThanu
Calcite | Level 5

Hi All,

 I want to find out the diffrerence between two date values as per that day wise..

 

example;

 

data have;
input id startdate enddate;
informat startdate enddate datetime20.;
format startdate enddate datetime20.;
cards;
1 1May2019:11:10:00 2May2019:00:30:00
;
run;

 

If I conider the time format as 12 hrs then the difference is 1hr 30 mins, 

1 hrs for 1st of may and 30 mins for 2 of may

like wise I want to find out the difference between two datatime values..

 

thanks in advance

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

How do you distinguishe 11am to 11pm reading your data like this? 

vThanu
Calcite | Level 5
Consider as 24 hrs time format..
cards;
1 1May2019:23:10:00 2May2019:00:30:00
;
run;
Tom
Super User Tom
Super User

The difference between two datetime values is a number of seconds.

dur_seconds = enddatetime - startdatetime;

To find the difference in DAYS convert them to date values first.

dur_days= datepart(enddatetime) - datepart(startdatetime);

Or divide the seconds by the number of seconds in a day.

dur_days = int(dur_seconds/'24:00't);

 

ScottBass
Rhodochrosite | Level 12

Your question isn't clear.  Please restate your logic as to why your sample data should return 1.5 hours.

 

Regardless, perhaps this helps...or not 🙂

 

data have;
input id startdate enddate;
informat startdate enddate datetime20.;
format startdate enddate datetime20.;
cards;
1 1May2019:23:10:00 2May2019:00:30:00
2 1May2019:23:10:00 5May2019:12:34:56
;
run;

data want;
   set have;
   day_diff=intck('dtday',startdate,enddate);
   hour_diff=intck('hour',startdate,enddate);   
   min_diff=intck('minute',startdate,enddate);
   sec_diff=intck('second',startdate,enddate);
   hours_frac1=min_diff/60;
   hours_frac2=sec_diff/(60*60);
   mins_frac=sec_diff/60;
run;

See the doc for additional options for the INTCK function.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
vThanu
Calcite | Level 5
Here we are getting difference between two date like 1 day or 80 mins so on…..
but what I am exactly looking is in that 80 mins (1May2019:23:10:00 - 2May2019:00:30:00)
50 mins in 1st of May and 30 mins for 2nd of May.

I am looking for the split, if the date/day changes then it should show into separately 50mins & 30 mins.

Hope you got my point.
Thanks in advance
Tom
Super User Tom
Super User

To find the time in the last day just use TIMEPART() on the datetime value.

To find the time left in a day the take difference between now and the beginning of tomorrow.

now=datetime();
rest_of_day = intnx('dtday',now,1,'B') - now;

Or perhaps between now and end of today?  

rest_of_day = intnx('dtday',now,0,'E') - now;

Play with it until you get total to be 80 minutes and not 79 minutes and 59 seconds or 80 minutes and 1 second.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 915 views
  • 3 likes
  • 4 in conversation