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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 420 views
  • 1 like
  • 4 in conversation