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

Hi all , How to convert 28-APR-20 02.56.42.897749 PM   ( length is 28 char) to DATETIME ?

Please can you help.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
s_lassen
Meteorite | Level 14

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.

ed_sas_member
Meteorite | Level 14

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,

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 645 views
  • 1 like
  • 4 in conversation