DATA Step, Macro, Functions and more

read data AAAA-MM-DD-HH.MM.SS.nnnnnn

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

read data AAAA-MM-DD-HH.MM.SS.nnnnnn

Hi I have to import in datatime SAS this datetime:

 

2011-02-28-23.59.59.999999

2015-05-22-11.21.17.558222

2015-05-22-12.13.30.386022

I try with this code:

data prova;
	a='2011-02-28-23.59.59.999999';
	b= input(a,ANYDTDTM26.);
	c=put(b,datetime.);
run;


But I get the missing value...
I can't find the correct informat for this type of data....
Any of you, has some suggestion?


Accepted Solutions
Solution
‎01-10-2017 08:59 AM
Contributor
Posts: 24

Re: read data AAAA-MM-DD-HH.MM.SS.nnnnnn

[ Edited ]
Posted in reply to error_prone

I think anyddtm. will truncate the milliseconds part.

 

Try this to precisely input the datetime.

 

data _null_;

 

    a='2011-02-28-23.59.59.999999';

    b=inputn(substr(a,1,10),'yymmdd10.')*24*60*60+inputn(substr(a,12),'time15.');

 

    put b datetime25.6;

run;

 

This will split the datetime into two parts and input them separately with the correct informat.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post


All Replies
Frequent Contributor
Posts: 149

Re: read data AAAA-MM-DD-HH.MM.SS.nnnnnn

Using anydtdtm. instead of anydtdtm26. seems to solve the issue.

Solution
‎01-10-2017 08:59 AM
Contributor
Posts: 24

Re: read data AAAA-MM-DD-HH.MM.SS.nnnnnn

[ Edited ]
Posted in reply to error_prone

I think anyddtm. will truncate the milliseconds part.

 

Try this to precisely input the datetime.

 

data _null_;

 

    a='2011-02-28-23.59.59.999999';

    b=inputn(substr(a,1,10),'yymmdd10.')*24*60*60+inputn(substr(a,12),'time15.');

 

    put b datetime25.6;

run;

 

This will split the datetime into two parts and input them separately with the correct informat.

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

Super User
Posts: 7,832

Re: read data AAAA-MM-DD-HH.MM.SS.nnnnnn

Convert the input data to a properly formatted ISO 8601 datetime value, and then use the proper informat:

data have;
input value_char $26.;
substr(value_char,11,1) = 'T';
substr(value_char,14,1) = ':';
substr(value_char,17,1) = ':';
value = input(value_char,e8601dt26.6);
format value datetime26.6;
cards;
2011-02-28-23.59.59.999999
2015-05-22-11.21.17.558222
2015-05-22-12.13.30.386022
;
run;

Instead of the 'T', you can also use a blank. But the 'T' conforms to the norm.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,799

Re: read data AAAA-MM-DD-HH.MM.SS.nnnnnn

[ Edited ]

SAS has an informat for that.  YMDDTTM.

 

data date;
   input date:ymddttm.;
   line=_infile_;
   format date datetime32.6;
   cards;
2011-02-28-23.59.59.999999
2015-05-22-11.21.17.558222
2015-05-22-12.13.30.386022
;;;;;
   run;
proc print;
   run;

 Capture.PNG

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 308 views
  • 5 likes
  • 5 in conversation