Desktop productivity for business analysts and programmers

String 26 to DATE - TIME

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

String 26 to DATE - TIME

Hi guys

 

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

 

Regards from Brazil


Accepted Solutions
Solution
‎08-16-2016 10:07 AM
Esteemed Advisor
Posts: 6,646

Re: String 26 to DATE - TIME

[ Edited ]

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Community Manager
Posts: 2,693

Re: String 26 to DATE - TIME

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.

Esteemed Advisor
Posts: 6,646

Re: String 26 to DATE - TIME

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.);
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Community Manager
Posts: 2,693

Re: String 26 to DATE - TIME

@KurtBremser'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 Smiley Happy

Occasional Contributor
Posts: 16

Re: String 26 to DATE - TIME

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

Solution
‎08-16-2016 10:07 AM
Esteemed Advisor
Posts: 6,646

Re: String 26 to DATE - TIME

[ Edited ]

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 16

Re: String 26 to DATE - TIME

Thank you very much guys.

 

untitled1.JPG

Community Manager
Posts: 2,693

Re: String 26 to DATE - TIME

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;
Occasional Contributor
Posts: 16

Re: String 26 to DATE - TIME

Got it Chris!! Thanks

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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