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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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