BookmarkSubscribeRSS Feed
camfarrell25
Quartz | Level 8

Hello,

 

I have the following values for date in character/string format and I wish to convert to date (datetime22.3) but no method seems to work.

A solution in PROC SQL would be ideal.

 

2016-03-15 12:05:29.222699


any ideas?

 

 

5 REPLIES 5
Tom
Super User Tom
Super User

The INFORMAT of anydtdte26. seems to do the trick.

 

795   data want ;
796     input @1 anydt anydtdtm26. @1 date yymmdd10. time time17.;
797     format anydt datetime. date date. time time.;
798     put (_all_) (=);
799   cards;

anydt=15MAR16:12:05:29 date=15MAR16 time=12:05:29

 

Kurt_Bremser
Super User

You might also consider using the e8601dt informat for this conversion:

data test;
x1 = "2016-03-15 12:05:29.222699";
x2 = input(x1,e8601dt26.6);
format x2 datetime22.3;
run;

proc print noobs;
run;

Result:

            x1                                    x2

2016-03-15 12:05:29.222699    15MAR2016:12:05:29.223
Tom
Super User Tom
Super User

Be careful using a decimal setting on an INFORMAT. That means that when in the input text doesn't have a period then imply a period. Probably not going to be a problem with time data, but could cause real confusion and wrong results when reading values that could be integers.

Kurt_Bremser
Super User

@Tom wrote:

Be careful using a decimal setting on an INFORMAT. That means that when in the input text doesn't have a period then imply a period. Probably not going to be a problem with time data, but could cause real confusion and wrong results when reading values that could be integers.


Yep. e8601dt26. (without the decimals) works just the same, the fractional part is still read correctly.

I'm just so used to clean input data (all our timestamps come from DB/2 unloads) that I don't notice such things.

Tom
Super User Tom
Super User

Trouble is that FORMAT and INFORMAT specifcations look similar, but the meaning of adding the decimal part to an INFORMAT is totally different than the meaing of adding it to a FORMAT.  It is just best to get in the habit of NOT adding a decimal part to an INFORMAT unless you specifically did not write the period when writting the text version of the data and you want SAS to divide those values by 10**D.

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