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
186089 | 08DEC2017 | 01JAN60:00:00:00 | ||
111247 | 31JUL2015 | 01JAN60:00:00:00 |
Please help me to solve it.
Thanks in advance!
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;
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;
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.