MLEN
MLEN

Reputation: 2561

Convert character string to datetime in SAS

How can I convert 2020-12-04T12:54:38.9215456+01:00 to a datetime in SAS? My solution below does not give any result.

data temp;
timestamp = '2020-12-04T12:54:38.9215456+01:00';
run;

data temp;
set temp;
date = substr(timestamp, 1, 10);
time = substr(timestamp, 12, 8);
date_and_time = cat(date, " ", time);
new_datetime = input(date_and_time, datetime24.);
format new_datetime datetime24.;
run;

Upvotes: 1

Views: 5420

Answers (2)

Kermit
Kermit

Reputation: 3117

Try the anydtdtm. informat.

data temp;
timestamp = '2020-12-04T12:54:38.9215456+01:00';
to_dt = input(timestamp, anydtdtm.);
format to_dt datetime20.;
run;

Result:
04DEC2020:12:54:38

Upvotes: 1

Ullas
Ullas

Reputation: 11556

I think you need the date part in DATE9. format.

Code

data temp;
ts='2020-12-04T12:54:38.9215456+01:00';
dt=put(input(substr(ts, 1, 10), yymmdd10.), date9.);
tm=substr(ts, 12, 8);
new_ts=input(cat(dt, ' ', tm), datetime24.);
format new_ts datetime24.;
run;

Upvotes: 1

Related Questions