I have a dataset with a string variable that has this appearance : "10/16/2007 22:37:30". I would like to convert this string to become a datetime value. I read that the datetime format is this 16Oct2007:22:37:30 but I was trying to create a date and a time variable from the string using the substrn function and then join them to create my datetime variable, all that in a data step
Im unable to do it, I though it would be a great idea to ask here about the pertinence of my technique before trying to make it work that way, do you guys have any tips on the best way to do this ?
Here's my solution. Use substr to extract the date part of the string and the time part of the string into two variables. Then use the input function with the mmddyy10. informat to convert the date string to a SAS date value, and with the time8. informat to convert the time string to a SAS time value.
Given a SAS date value and a SAS time value, the SAS documentation for the DHMS function explains how to combine them into a SAS datetime value.
meanwhile someone also provided me this way of acheiving the desired result using the scan function instead of substr :
This works :
Data _null_ ;
%let entry_date_time_orig = "10/13/2007 01:14:35" ;
entry_date_time=sum((input(scan(&entry_date_time_orig.,1,' '),mmddyy10.)*60*60*24),input(scan(&entry_date_time_orig.,2,' '),time8.));