BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

Can soomeone help me to convert character datetime value to numeric date? Desired output is '01DEC2021'd which is in date9. format

 

data test;
a='2021-12-01 21:56:59.0000000';
b=input(a,27.);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19
data test;
a='2021-12-01 21:56:59.0000000';
d=input(scan(a,1,' '),yymmdd10.);
format d date9.;
run;

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19
data test;
a='2021-12-01 21:56:59.0000000';
d=input(scan(a,1,' '),yymmdd10.);
format d date9.;
run;
Kurt_Bremser
Super User

You can apply the YYMMDD10. informat directly, as it will only read the first 10 characters:

data test;
a = '2021-12-01 21:56:59.0000000';
b = input(a,yymmdd10.);
format b yymmdd10.;
run;

To read the whole string as a datetime, use the E8601DT. informat:

data test;
a = '2021-12-01 21:56:59.0000000';
b = input(a,e8601dt26.);
c = datepart(b);
format
  b e8601dt26.6
  c yymmdd10.
;
run;
ballardw
Super User

I have a lot of files that apparently originate in not-well set up databases where I get date values that come as datetime values like that, some times as many as 30 date fields per record. Every single "time" component is 00:00:00. So using just the date format to read the date part makes lots of sense and I do it in the data step to read the file instead of waiting to fix it later.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 848 views
  • 6 likes
  • 4 in conversation