Hi all , How to convert 28-APR-20 02.56.42.897749 PM ( length is 28 char) to DATETIME ?
Please can you help.
See
data want;
string = "28-APR-20 02.56.42.897749 PM";
date = input(scan(string,1," "),date9.);
time = input(substr(string,11),time20.);
datetime = dhms(date,0,0,time);
format
date yymmddd10.
time time15.6
datetime e8601dt26.6
;
run;
See
data want;
string = "28-APR-20 02.56.42.897749 PM";
date = input(scan(string,1," "),date9.);
time = input(substr(string,11),time20.);
datetime = dhms(date,0,0,time);
format
date yymmddd10.
time time15.6
datetime e8601dt26.6
;
run;
You can also wrap up the solution by @Kurt_Bremser in a macro function:
%macro dt_AMPM(str);
%local time date;
%let date=input(&str,date9.);
%let time=input(substr(&str,11,time20.);
dhms(&date,0,0,&time)
%mend;
This function can then be used in a data step:
data want;
set have;
datetime=%dt_AMPM(date_str);
format datetime datetime25.6;
run;
Which may be a nice solution if you have more than one datetime string in this format.
Hi @dennis_oz
You can try informat datetime30.6 to keep the highest precision:
data want;
string = "28-APR-20 02.56.42.897749 PM";
string_n = input(string, DATETIME30.6);
format string_n e8601dt26.6;
run;
Best,
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.