SAS Programming

DATA Step, Macro, Functions and more
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.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1486 views
  • 6 likes
  • 4 in conversation