turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- number of days between datetime observations

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-26-2010 06:11 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-26-2010 09:46 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-27-2010 03:07 AM

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.

Scott Barry

SBBWorks, Inc.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-07-2010 04:15 PM

@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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-07-2010 04:22 PM

Yes, Chang you are correct.

Scott's (sbb) advice is not appropriate.

Scott's (sbb) advice is not appropriate.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-07-2010 05:15 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-07-2010 06:32 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-28-2010 06:27 PM

Looks good. Thank you.