BookmarkSubscribeRSS Feed
gzr2mz39
Quartz | Level 8
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.
7 REPLIES 7
NickR
Quartz | Level 8
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;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
chang_y_chung_hotmail_com
Obsidian | Level 7
@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.
gzr2mz39
Quartz | Level 8
Yes, Chang you are correct.
Scott's (sbb) advice is not appropriate.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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;
ArtC
Rhodochrosite | Level 12
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.
gzr2mz39
Quartz | Level 8
Looks good. Thank you.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1753 views
  • 0 likes
  • 5 in conversation