DATA Step, Macro, Functions and more

number of days between datetime observations

Reply
Regular Contributor
Posts: 199

number of days between datetime observations

I would like to see how many days (to the nearest 0.1) the following four observations are a part from each other (eg how many days are there between '11OCT2010:10:28:47' and '17OCT2010:12:21:47'?).
This variable x would show, for example, ., 2.0, 6.1, 10.2 .
I'm using datetime20. to format my datetime variable.
'09OCT2010:11:28:47'
'11OCT2010:10:28:47'
'17OCT2010:12:21:47'
'27OCT2010:16:14:47'
Thank you.
Frequent Contributor
Posts: 81

Re: number of days between datetime observations

data one;
input;
date = input(_infile_,datetime20.);
format date datetime20.;
datalines;
09OCT2010:11:28:47
11OCT2010:10:28:47
17OCT2010:12:21:47
27OCT2010:16:14:47
run;

data two;
set one;
retain ndate;
if n(ndate) then x=round((date-ndate)/(3600*24),0.1);
ndate = date;
drop ndate;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: number of days between datetime observations

SAS provides the INTCK function to compare two dates. Also, the LAG function can be used to interrogate SAS variables between observations.

Scott Barry
SBBWorks, Inc.
Regular Contributor
Posts: 241

Re: number of days between datetime observations

@Scott: Technically speaking, INTCK function is *not* appropriate for calculating the duration between two time points, especially the unit of time you use is larger than the accuracy of the time points you have. For example, I don't think it is appropriate to measure the duration between two datetimes (measured at the second unit) using a function call like INTCK('day',...). The reason being that the INTCK function enumerates the *boundaries* in the given interval. This means that in terms of 'DAY' interval (without any shifts), the INTCK counts how many midnights(00:00am) there are between the two given time points, which may be somewhat different from the counts of how many 24-hours between the two given time points.

In this case, OP is clear about what s/he wants -- the duration --, thus I would simple calculate it by subtracting the start from the finish (this will give the duration in terms of seconds). If the OP is asked to report it in other time units(minutes, hours, days), then a simple division/multiplication will do.
Regular Contributor
Posts: 199

Re: number of days between datetime observations

Posted in reply to chang_y_chung_hotmail_com
Yes, Chang you are correct.
Scott's (sbb) advice is not appropriate.
Super Contributor
Super Contributor
Posts: 3,174

Re: number of days between datetime observations

As many SAS programmers know, there are numerous ways to get to a desired result - I offered one that was somewhat generic to offer technical direction using available SAS functions / facilities.

From what I understood, the OP was interested in calculating "equivalent days between two datetime values", so my recommendation (one approach) was to take advantage of available SAS function INTCK -- here is an expression that will yield the correct result, using two SAS DATETIME variables (I have them coded as literals for over-simplification).

Scott Barry
SBBWorks, Inc.

data _null_;
equiv_days = round( intck('dtsecond','30jan2010:12:00'dt,'15mar2010:13:00'dt) / '24:00:00't , 0.1);
putlog _all_;
run;
Valued Guide
Posts: 634

Re: number of days between datetime observations

Do not forget that the INTCK and INTNX functions can also make use of interval multipliers and shift operators. Also both now have alignment options to refine the value that is to be returned.
Regular Contributor
Posts: 199

Re: number of days between datetime observations

Looks good. Thank you.
Ask a Question
Discussion stats
  • 7 replies
  • 203 views
  • 0 likes
  • 5 in conversation