Hello-,
I have the data below. T1 and T2 are DATETIME format with ANYDTDTM40 informat. For an unknown reason, T3 appears as a character. All should be DateTime. I was to first make T3 be same format and perform the following subtractions with results in hours:
data ERT;
set ERT;
diff1 = (T1 - T2) / 60;
Diff2 (T2-T2)/60;
diff3 = (T1 - T3) / 60;
run;
The SAS System |
Obs | T1 | T2 | T3 |
---|---|---|---|
1 | 1/3/18 6:04 PM | 1/3/18 1:30 PM | 1/3/18 4:28 PM |
2 | |||
3 | 1/16/18 3:38 AM | 1/15/18 10:05 AM | 1/16/18 2:22 AM |
4 | 2/7/18 6:54 PM | 2/7/18 3:15 PM | 2/7/18 5:19 PM |
5 | 2/16/18 1:14 PM | 2/15/18 10:30 PM | 2/16/18 1:03 PM |
6 | 3/6/18 3:21 PM | 3/5/18 8:20 AM | 3/6/18 2:53 PM |
7 | 3/19/18 3:23 PM | 3/19/18 9:30 AM | 3/19/18 2:01 PM |
8 | 3/22/18 11:51 AM | 3/21/18 8:05 AM | 3/22/18 11:22 AM |
9 | 3/23/18 1:18 PM | 3/22/18 7:40 PM | 3/23/18 11:27 AM |
10 | 5/20/18 4:59 AM | 5/19/18 10:30 PM | 5/20/18 4:24 AM |
do proc contents to see what type t3 is. and for subtracting 2 date times, I usually just divide the difference by 3600 to get seconds.
If T3 is character, then you need to convert it to numeric.
t3n = input(t3,mdyampm.);
However, dividing the differences by 60 does not compute hours, since the date/time values are in seconds. There are 60 seconds in a minute; so you need to divide by the number of seconds in an hour.
Thank you, The first goal is to get the difference. If it is hours, minutes or seconds, that can easily be achieved. However, the difference is not coming up"
data ERT;
set ERT;
t31 = input(t3,mdyampm.);
run;
data ERT;
set ERT;
diff1 = (t1 - t2);
diff2 = (t2 - t31);
diff3 = (t1 - t31) ;
run;
proc means data=ERT;
var diff1 diff2 diff3;
run;
Variable | N | Mean | Std Dev | Minimum | Maximum | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|
Show us the LOG (all of it, every single line) for this code.
Please copy the log as text and paste it into the window that appears when you click on the </> icon
Also, show us the output from PROC CONTENTS for data set ERT.
The output you showed does not look like what the DATETIME format displays.
Are you sure you have Numeric variables with the DATETIME format attached (the INFORMAT attached to the variables is of no importance once the values are already in the variable)?
SAS stores datetime values in seconds. So the difference is in seconds. If you want to convert seconds to hours just divide by the number of seconds in an hour.
data want;
set have;
seconds = t2 - t1;
hours = seconds / '01:00:00't ;
run;
do proc contents to see what type t3 is. and for subtracting 2 date times, I usually just divide the difference by 3600 to get seconds.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.