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

Hello, I'm trying to reformat a date (ddmmmyyy) to DATETIME.  The code I tried to use didn't work:

 

data WORK.DEMOS1;
set WORK.DEMOS;
DateofBirth1=input(DateofBirth,date9.);
format DateofBirth1 datetime16.;
run;

 

Does anyone know how to do this in a data step or proc sql step? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot "reformat" a date to a datetime.  You need to convert the date value (number of days) to a datetime value (number of seconds).  Then you can use any of the many datetime formats to change how the number is displayed.  You must pick a time of day for the datetime value.  Most people use midnight (00:00:00).

data WORK.DEMOS1;
  set WORK.DEMOS;
  DateTimeofBirth=dhms(DateofBirth,0,0,0);
  format DateTimeofBirth datetime20.;
run;

Note: Do not display date or datetime values with only 2 digits for the year.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You cannot "reformat" a date to a datetime.  You need to convert the date value (number of days) to a datetime value (number of seconds).  Then you can use any of the many datetime formats to change how the number is displayed.  You must pick a time of day for the datetime value.  Most people use midnight (00:00:00).

data WORK.DEMOS1;
  set WORK.DEMOS;
  DateTimeofBirth=dhms(DateofBirth,0,0,0);
  format DateTimeofBirth datetime20.;
run;

Note: Do not display date or datetime values with only 2 digits for the year.

novinosrin
Tourmaline | Level 20
/*If DateofBirth is charatcer*/
data WORK.DEMOS1;
set WORK.DEMOS;
DateofBirth1=dhms(input(DateofBirth,date9.),0,0,0);
format DateofBirth1 datetime16.;
run;
/*If DateofBirth is numeric sas data*/
data WORK.DEMOS1;
set WORK.DEMOS;
DateofBirth1=dhms(DateofBirth,0,0,0);
format DateofBirth1 datetime16.;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 883 views
  • 0 likes
  • 3 in conversation