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
How do you distinguishe 11am to 11pm reading your data like this?
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);
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.