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.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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