Solved
Contributor
Posts: 74

Convert Integer Datetime to Datetime format

Hi,

May I ask how can I convert the datetime data which is an integer into datetime format? I've tried many options but still it shows the same or the data becomes periods (.....).

These are my data:

lngIndatetime
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608

I would like to look like 20170405054608 = April 5, 2017 05:46:08.

Thank you!

Accepted Solutions
Solution
‎09-13-2017 04:23 AM
Valued Guide
Posts: 561

Re: Convert Integer Datetime to Datetime format

I think the final substr is wrong - should be

``````data have;
input lngIndatetime;
format lngIndatetime 20.;
datalines;
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608
;

data want(drop=_dt CharDT);
set have;
CharDT = put(20170331104215, 14.);
_dt = cats(substr(CharDT,1,8),"T",substr(CharDT,9,6));
dt = input(_dt, b8601dt15.);
format dt datetime20.;
run;``````

All Replies
PROC Star
Posts: 1,190

Re: Convert Integer Datetime to Datetime format

Is lngIndatetime character or numeric?

Contributor
Posts: 74

Re: Convert Integer Datetime to Datetime format

Hi @draycut

lngIndatetime is character.

Super User
Posts: 9,611

Re: Convert Integer Datetime to Datetime format

Use substr() to extract relevant parts of your character string, input() to convert to numeric, and dhms() to convert to a datetime value:

``````data have;
input lngIndatetime :\$14.;
cards;
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608
;
run;

data want;
set have;
lngIndatetime_num = dhms(
input(substr(lngIndatetime,1,8),yymmdd8.),
input(substr(lngIndatetime,9,2),2.),
input(substr(lngIndatetime,11,2),2.),
input(substr(lngIndatetime,13,2),2.)
);
format lngIndatetime_num datetime19.;
run;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 74

Re: Convert Integer Datetime to Datetime format

Hi @KurtBremser,

I'm getting error when running the given code. The data is in numeric format.

Thank you!
PROC Star
Posts: 1,190

Re: Convert Integer Datetime to Datetime format

[ Edited ]
``````data have;
input lngIndatetime \$15.;
datalines;
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608
;

data want(drop=_dt);
set have;
_dt = cats(substr(lngIndatetime,1,8),"T",substr(lngIndatetime,9,6));
dt = input(_dt, b8601dt15.);
format dt datetime20.;
run;``````
Super User
Posts: 9,611

Re: Convert Integer Datetime to Datetime format

jei wrote:
Hi @KurtBremser,

I'm getting error when running the given code. The data is in numeric format.

Thank you!

PLEASE POST THE ******* LOG WHEN REPORTING AN ERROR!!!

Just to quote yourself:

"

Hi @draycut

lngIndatetime is character."

Post your real data in a data step, as described a gazillion times on this forum.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 1,190

Re: Convert Integer Datetime to Datetime format

[ Edited ]

Dirty correction to my previous code should to

``````data have;
input lngIndatetime;
format lngIndatetime 20.;
datalines;
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608
;

data want(drop=_dt CharDT);
set have;
CharDT = put(20170331104215, 14.);
_dt = cats(substr(CharDT,1,8),"T",substr(CharDT,9,6));
dt = input(_dt, b8601dt15.);
format dt datetime20.;
run;
``````
Solution
‎09-13-2017 04:23 AM
Valued Guide
Posts: 561

Re: Convert Integer Datetime to Datetime format

I think the final substr is wrong - should be

``````data have;
input lngIndatetime;
format lngIndatetime 20.;
datalines;
20170331104215
20170401154920
20170401190015
20170401190014
20170402123848
20170402162202
20170402162202
20170405051751
20170405051437
20170405054608
;

data want(drop=_dt CharDT);
set have;
CharDT = put(20170331104215, 14.);
_dt = cats(substr(CharDT,1,8),"T",substr(CharDT,9,6));
dt = input(_dt, b8601dt15.);
format dt datetime20.;
run;``````
PROC Star
Posts: 1,190