BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

In my data set I have two datetime variables in numeric format. I need to calculate difference between the datetime and put the difference into three variables, Forexample,

 

variable1                   variable2

22oct15:10:47:00       21oct15:10:00:02

 

the difference between  variable1-variable2=1day 0 hour 47min into three variables  days hour min

                                                                                                                                       1        0      47

 

I am using the following code. 

data one;
set two;

 LDDAYS= variable1-variable2;run;

 

this code is not giving me expected values. Thank you

6 REPLIES 6
Kurt_Bremser
Super User

datetime values area stored in SAS as numbers, counting seconds from 01jan1960:00:00:00.

Therefore your initial subtraction will give you another datetime value, which will probably be in early January, 1960, if you assign it a datetime format.

I suggest this:

diff = variable1 - variable2; * difference in seconds;
lddays = int(diff / 86400); * days;
difftime = diff - lddays * 86400; * subtract the days;
format difftime time8.; * display as time value;

 

knveraraju91
Barite | Level 11

Thank you. That worked. But I need to separate hours and minutes into two variables.

Forexample I got difftime(In redink below code) 1:15:48. I need to place the 1 in hour variable and round of 15.48=16 in Min variable. Thanks for your quick response.

 

diff = variable1 - variable2; * difference in seconds;
lddays = int(diff / 86400); * days;
difftime = diff - lddays * 86400; * subtract thedays;
format difftime time8.; * display as time vale;

 

Kurt_Bremser
Super User

Well, you use the same principle. Use factor 3600 to extract the hours, and 60 to extract the minutes. Use the round() function on the minutes and assign simple numeric formats to the target variables.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would also suggest, that rather than using a set number, which can be tricky to read if you don't know what that number represents, you can use the SAS functions -> year(), month(), day(), hour(), minute() to extract parts from a date/datetime and use those.  

 

data want;
  variable1="22oct15:10:47:00"dt; variable2="21oct15:10:00:02"dt;
  difference_in_days=datepart(variable1)-datepart(variable2);
  difference_in_hours=hour(timepart(variable1)-timepart(variable2));
  difference_in_minutes=minute(timepart(variable1)-timepart(variable2));
  format variable1 variable2 datetime.;
run;
Kurt_Bremser
Super User

RW9 wrote:
data want;
  variable1="22oct15:10:47:00"dt; variable2="21oct15:10:00:02"dt;
  difference_in_days=datepart(variable1)-datepart(variable2);
  difference_in_hours=hour(timepart(variable1)-timepart(variable2));
  difference_in_minutes=minute(timepart(variable1)-timepart(variable2));
  format variable1 variable2 datetime.;
run;

When doing this, one needs to take care of cases where timepart(variable1)-timepart(variable2) turns out to be negative, and reduce the days by 1.

ie

variable1='27jan2016:09:23:15'dt;
variable2='26jan2016:15:45:20'dt;
Ksharp
Super User

Here is the solution from SAS-L

https://listserv.uga.edu/cgi-bin/wa?A0=SAS-L

 


proc format; 
  picture durt 
  other='%0nD %0H:%0M:%0s' (datatype=time); 
run; 

proc fcmp outlib=work.func.fmt;
   function xx(c) $;
      length fmt $ 20;
      day=int(divide(c,60*60*24));
      return (put(day,z2.) || 'D ' || put(c,tod.));
   endsub;
run;
options cmplib=(work.func);
proc format;
   value new(default=20) other=[xx()];
 run;
data _null_; 
 
   t= 368000; 
     put t durt. / t new.; 
run; 
OUTPUT:
   4D 06:13:20
 04D 06:13:20 

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