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

Hi guys

 

 How can I convert this 26 lenght string "2016-08-11-15.48.53.216961" to date time format?

 

Regards from Brazil

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Ah, I overlooked that one. The dots in the time need to be replaced with colons:

data test;
invar = '2016-08-11T15.48.53.216961';
format outvar datetime25.6;
substr(invar,14,1) = ':';
substr(invar,17,1) = ':';
outvar = input(invar,e8601dt26.6);
run;

View solution in original post

8 REPLIES 8
ChrisHemedinger
Community Manager

I'm not sure that's a standard datetime format.  The closest match I see is the E8601DTw.d informat.  Find the list of ISO date/time formats and informats here.

 

data test;
  length dt 8;
  format dt datetime20.;
  /* different than 2016-08-11-15.48.53.216961 */
  dt = input("2016-08-11T15:48:53.216961", E8601DT26.);
run;

If that doesn't work, you might need to parse out the pieces and build your own datetime value in piecemeal.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Kurt_Bremser
Super User

The simplest option is to replace the dash after the date with a 'T' or a blank ('T' is better, as it conforms to the ISO standard) and use the e8601dt informat for conversion.

format outvar datetime25.6;
substr(invar,11,1) = 'T';
outvar = input(invar,e8601dt26.);
ChrisHemedinger
Community Manager

@Kurt_Bremser's approach is so much simpler than parsing out the pieces! I had been working on an alternative that coerced the value by replacing the '-' with the 'T' and various '.' with ':' -- but Kurt's is much more elegant so I won't bother to share 🙂

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

Am I doing something wrong?

 

data A;
set A;
/*DATA_HORA_INCLUSAO_TEMP*/
format outvar datetime25.6;
substr(DATA_HORA_INCLUSAO_TEMP,11,1) = 'T';
outvar = input(DATA_HORA_INCLUSAO_TEMP,e8601dt26.);
run;

NOTE: Invalid argument to function INPUT at line 20 column 10.

 

untitled.JPG

Kurt_Bremser
Super User

Ah, I overlooked that one. The dots in the time need to be replaced with colons:

data test;
invar = '2016-08-11T15.48.53.216961';
format outvar datetime25.6;
substr(invar,14,1) = ':';
substr(invar,17,1) = ':';
outvar = input(invar,e8601dt26.6);
run;

Thank you very much guys.

 

untitled1.JPG

ChrisHemedinger
Community Manager

Ha! I should have kept going and posted my approach!

 

data test;
  length dt 8 dt2 8 fixdate $ 26 origdate $ 26;
  format dt datetime26.6 dt2 datetime26.6;

  /* using properly formatted input */
  dt = input("2016-08-11T15:48:53.216961", E8601DT26.);
 
  /* supplied input */
  origdate = "2016-08-11-15.48.53.216961";

  /* coerce the incoming date to a proper format */
  fixdate = substr(origdate,1,10) || 'T' 
      || translate(substr(origdate,12,8),':','.') || '.'
      || substr(origdate,21,6); 

  dt2 = input(fixdate, E8601DT26.);
 
run;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

Got it Chris!! Thanks

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 2392 views
  • 10 likes
  • 3 in conversation