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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
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 🙂

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

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;
Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

Got it Chris!! Thanks

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 3006 views
  • 10 likes
  • 3 in conversation