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

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:MM:SS.  Any help would be appreciated.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
cmcquain
Fluorite | Level 6
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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
cmcquain
Fluorite | Level 6
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
cmcquain
Fluorite | Level 6
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.))

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3426 views
  • 0 likes
  • 2 in conversation