I wish to compute the difference between the following two timepoints, that is Time2-Time2. But the time are characters, that is Char $16. I was expecting it to be Datetime. Anydtdtm40 but they are not. Please can you show me how to convert but Time1 and Time2 from Char $16 to Datetime. Anydtdtm40 and perform the subtraction.
Time1 | Time2 | |
1/3/18 1:30 PM | . | |
. | . | |
1/15/18 10:05 AM | 1/16/18 1:31 AM | |
2/7/18 3:15 PM | . | |
2/15/18 10:30 PM | 2/16/18 11:50 AM | |
3/5/18 8:20 AM | 3/6/18 2:02 AM | |
3/19/18 9:30 AM | 3/19/18 1:55 PM |
@desireatem wrote:
I wish to compute the difference between the following two timepoints, that is Time2-Time2. But the time are characters, that is Char $16. I was expecting it to be Datetime. Anydtdtm40 but they are not. Please can you show me how to convert but Time1 and Time2 from Char $16 to Datetime. Anydtdtm40 and perform the subtraction.
Time1 Time2 1/3/18 1:30 PM . . . 1/15/18 10:05 AM 1/16/18 1:31 AM 2/7/18 3:15 PM . 2/15/18 10:30 PM 2/16/18 11:50 AM 3/5/18 8:20 AM 3/6/18 2:02 AM 3/19/18 9:30 AM 3/19/18 1:55 PM
In a data step create two new variables explicitly using the Anydtdtm informat. An example;
data junk; x="1/3/18 1:30 PM"; y=input(x,anydtdtm32.); format y datetime18.; run;
Depending on how you read the data SAS may not assume that mixes of 1 digit and 2 digit values with slashes and spaces with text like PM are actually date time values.
@desireatem wrote:
I wish to compute the difference between the following two timepoints, that is Time2-Time2. But the time are characters, that is Char $16. I was expecting it to be Datetime. Anydtdtm40 but they are not. Please can you show me how to convert but Time1 and Time2 from Char $16 to Datetime. Anydtdtm40 and perform the subtraction.
Time1 Time2 1/3/18 1:30 PM . . . 1/15/18 10:05 AM 1/16/18 1:31 AM 2/7/18 3:15 PM . 2/15/18 10:30 PM 2/16/18 11:50 AM 3/5/18 8:20 AM 3/6/18 2:02 AM 3/19/18 9:30 AM 3/19/18 1:55 PM
In a data step create two new variables explicitly using the Anydtdtm informat. An example;
data junk; x="1/3/18 1:30 PM"; y=input(x,anydtdtm32.); format y datetime18.; run;
Depending on how you read the data SAS may not assume that mixes of 1 digit and 2 digit values with slashes and spaces with text like PM are actually date time values.
I really do not like the ANY... informats, as they might happily import something unexpected as a valid value, or misinterpret data because of the locale setting. Mind that several English-speaking countries use a DMY date and not MDY as the US does.
So be strict:
data have;
infile datalines dlm="," truncover;
input (time1 time2) (:$16.);
datalines;
1/3/18 1:30 PM,.
.,.
1/15/18 10:05 AM,1/16/18 1:31 AM
2/7/18 3:15 PM,.
2/15/18 10:30 PM,2/16/18 11:50 AM
3/5/18 8:20 AM,3/6/18 2:02 AM
3/19/18 9:30 AM,3/19/18 1:55 PM
;
data want;
set have;
array in {*} time1 time2;
array out {*} t1 t2;
format t1 t2 e8601dt19.;
do i = 1 to 2;
if in{i} ne " "
then do;
d = input(scan(in{i},1," "),mmddyy10.);
t = input(scan(in{i},2," "),time5.);
if scan(in{i},3," ") = "PM" and t ne 0 then t = t + '12:00't;
out{i} = dhms(d,0,0,t);
end;
end;
drop d t i;
run;
Any unexpected value will cause a NOTE in the log.
And I won't elaborate on the intelligence (or lack thereof) of using 2-digit years.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.