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!
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;
Is lngIndatetime character or numeric?
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;
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;
@jei wrote:
Hi @Kurt_Bremser,
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.
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;
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;
Of course. Good catch @ChrisBrooks 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.