BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi community 

 

I import the dataset from excel which contains date values in the format of date9.

Once i tried to convert it to DATETIME format it look like

 

data a1;
set b1;
rec_date = datepart(Record_date);
format rec_date datetime.;
run;

SAS Output

Obs Patient_ID Record_Date     rec_date12
18608908DEC2017  01JAN60:00:00:00
11124731JUL2015  01JAN60:00:00:00

 

Please help me to solve it.

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

You have to do it the opposite way. The DATEPART function converts datetimes to dates, what you want is the opposite, try with DHMS:

data a1;
  set b1;
  rec_date = dhms(Record_date,0,0,0);
  format rec_date datetime.;
run;

View solution in original post

3 REPLIES 3
s_lassen
Meteorite | Level 14

You have to do it the opposite way. The DATEPART function converts datetimes to dates, what you want is the opposite, try with DHMS:

data a1;
  set b1;
  rec_date = dhms(Record_date,0,0,0);
  format rec_date datetime.;
run;
Kurt_Bremser
Super User

Datetimes (and times) are counts of seconds, dates are counts of days.

So, roughly

datetime = date * 86400

(86400 seconds per day)

The datepart function basically does

floor(datetime / 86400)

So when you use it on a date (which today means 21453), you get zero, and zero as a datetime is midnight on 1960-01-01.

data_null__
Jade | Level 19

It is possible that your date value from excel has the time part as a decimal.

 

125  data _null_;
126     date = today() + .32;
127     put 'NOTE: ' date=date9. date=;
128     datetime = dhms(date,0,0,0);
129     put 'NOTE: ' datetime=datetime19.;
130     run;

NOTE: date=26SEP2018 date=21453.32
NOTE: datetime=26SEP2018:07:40:48

sas-innovate-wordmark-2025-midnight.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. Sign up by March 14 for just $795.


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
  • 3465 views
  • 4 likes
  • 4 in conversation