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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 270 views
  • 0 likes
  • 3 in conversation