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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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