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
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;
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;
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.
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;
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;
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.