DATA Step, Macro, Functions and more

Convert Integer Datetime to Datetime format

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 75
Accepted Solution

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: 596

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;

View solution in original post


All Replies
PROC Star
Posts: 1,411

Re: Convert Integer Datetime to Datetime format

Is lngIndatetime character or numeric?

Frequent Contributor
Frequent Contributor
Posts: 75

Re: Convert Integer Datetime to Datetime format

Hi @draycut

 

lngIndatetime is character.

Super User
Posts: 10,626

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
Frequent Contributor
Frequent Contributor
Posts: 75

Re: Convert Integer Datetime to Datetime format

Posted in reply to KurtBremser
Hi @KurtBremser,

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

Thank you!
PROC Star
Posts: 1,411

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: 10,626

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."

 

Please make up your mind.

 

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,411

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: 596

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,411

Re: Convert Integer Datetime to Datetime format

Posted in reply to ChrisBrooks

Of course. Good catch @ChrisBrooks Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 582 views
  • 1 like
  • 4 in conversation