BookmarkSubscribeRSS Feed
iSAS
Quartz | Level 8

I have a character variable with parameters format=$char19. informat=$char19. that I would would like to convert to datetime. Below are the examples of variables:

 

Aug 24 2017 12:11PM

Dec  7 2017  2:12PM

May 28 2014  3:55PM

 

I believe I only need to use the input function. May I know what informat should I use? I tried these but it seems that I'm getting the wrong value

 

numeric=input('Feb 6 2015 4:51PM',ANYDTDTM19.);

numeric=input('Feb 6 2015 4:51PM',ANYDTTME19.);

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am pretty sure that some people go out of their way to make dates as difficult to read as possible.  I would do:

data have;
  text="Aug 24 2017 12:11PM"; output;
  text="Dec  7 2017  2:12PM"; output;
run;

data want (drop=tmp_:);
  set have;
  tmp_date=input(cats(substr(text,5,2),substr(text,1,3),substr(text,8,4)),date9.);
  tmp_time=input(strip(substr(text,12,6)),time5.);
  want=dhms(tmp_date,hour(tmp_time),minute(tmp_time),0);
  format want datetime.;
run;

Well, I would probably go back to source and change that into a usable format myself...

Kurt_Bremser
Super User

Your idea of using the anydtdtm informat works:

data want;
input indate anydtdtm19.;
format indate dateampm22.;
cards;
Aug 24 2017 12:11PM
Dec  7 2017  2:12PM
May 28 2014  3:55PM
;
run;

proc print data=want noobs;
run;

Result:

               indate

24AUG2017:12:11:00 PM
07DEC2017:02:12:00 PM
28MAY2014:03:55:00 PM

 

Edit: increased length of display format from 20 to 22 to get a 4-digit year.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 1077 views
  • 0 likes
  • 3 in conversation