BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

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  

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.

View solution in original post

3 REPLIES 3
jimbarbour
Meteorite | Level 14
I would use Informat AnydtdtmN where N is the length of the characters that comprise the date time value. You can use AnydtdtmN as part of an INPUT statement or you can use the Input function after you've successfully read in the character data.

Jim
ballardw
Super User

@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.

Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1104 views
  • 0 likes
  • 4 in conversation