BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jei
Quartz | Level 8 jei
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Is lngIndatetime character or numeric?

jei
Quartz | Level 8 jei
Quartz | Level 8

Hi @PeterClemmensen

 

lngIndatetime is character.

Kurt_Bremser
Super User

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;
jei
Quartz | Level 8 jei
Quartz | Level 8
Hi @Kurt_Bremser,

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

Thank you!
PeterClemmensen
Tourmaline | Level 20
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;
Kurt_Bremser
Super User

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

PeterClemmensen
Tourmaline | Level 20

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;
ChrisBrooks
Ammonite | Level 13

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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