Desktop productivity for business analysts and programmers

Change text to datetime

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Change text to datetime

I am using SAS Enterprise Guide and I am trying to format a text field by the name of 'Admit Date' with data such as: 20180110164700 to a datetime format.  Hopefully formatted to YYYY-MM-DD HH:MMSmiley FrustratedS.  Any help would be appreciated.

 

Thank you


Accepted Solutions
Solution
‎01-12-2018 11:48 AM
Occasional Contributor
Posts: 8

Re: Change text to datetime

I got it! I had to change substring count on minutes and seconds.

DHMS(input(substr(t3.'Admit Date'n,1,8),yymmdd8.),input(substr(t3.'Admit Date'n,9,2),hh2.),input(substr(t3.'Admit Date'n,11,2),mm2.),input(substr(t3.'Admit Date'n,13,2),ss2.))

View solution in original post


All Replies
Super User
Super User
Posts: 9,840

Re: Change text to datetime

Well, in code you do (and this assumes I am right on what the date should be - really a good idea to get the sender to apply some sort of standard date format to the data, something like ISO dates):

data want;
  set have;
  mydt=dhms(input(substr(text,1,8),yymmdd8.),input(substr(text,9,2),best.),input(substr(text,10,2),best.),input(substr(text,12,2),best.));
  format mydt datetime.;
run;
Occasional Contributor
Posts: 8

Re: Change text to datetime

I had to modify your code some as shown DHMS(input(substr(t3.'Admit Date'n,1,8),yymmdd8.),input(substr(t3.'Admit Date'n,9,2),hh2.),input(substr(t3.'Admit Date'n,10,2),mm2.),input(substr(t3.'Admit Date'n,12,2),ss2.)) Format = DATETIME19. I am still getting the wrong output. From the original 20180110164700 I am getting this 10JAN2018:17:05:10 when the field should look like 10JAN2018:16:47:00
Solution
‎01-12-2018 11:48 AM
Occasional Contributor
Posts: 8

Re: Change text to datetime

I got it! I had to change substring count on minutes and seconds.

DHMS(input(substr(t3.'Admit Date'n,1,8),yymmdd8.),input(substr(t3.'Admit Date'n,9,2),hh2.),input(substr(t3.'Admit Date'n,11,2),mm2.),input(substr(t3.'Admit Date'n,13,2),ss2.))
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 277 views
  • 0 likes
  • 2 in conversation