Hello experts...
I have dates like below in an excel file.
11Nov2014 04:41:32 |
20Dec2015 17:26:28 |
28Dec2015 17:26:55 |
21Dec2015 17:28:40 |
19Apr2015 20:05:38 |
10May2014 03:43:47 |
Once I imported this sheet it is converting to a character variable and dates changed like below
41662.26309467592
41366.85961716435
41367.70266952546
41354.89744355325
How to convert these into an ISO date? I read some where that we need to add "00:00" and convert but its not working. Please suggest.
Thanks in advance.
Assuming the values are character type already, use the INPUT function to convert them to numbers, then some math to convert from Excel datetimes to SAS datetimes. Here's an example with one hardcoded value.
data a;
excel_date = input("41954.195509259",16.9);
sas_date = (excel_date - 21916) * 86400;
format sas_date is8601dt.;
run;
Result:
2014-11-11T04:41:32
Importing already done, I can't change anything in importing now. 😞
I have to change these numbers into ISO dates using SAS programming only
Assuming the values are character type already, use the INPUT function to convert them to numbers, then some math to convert from Excel datetimes to SAS datetimes. Here's an example with one hardcoded value.
data a;
excel_date = input("41954.195509259",16.9);
sas_date = (excel_date - 21916) * 86400;
format sas_date is8601dt.;
run;
Result:
2014-11-11T04:41:32
Thank you very much Chris.. you r awesome..
what are these numbers 21916 & 86400?
can I use these numbers for any date? how do you get these numbers.. sorry I am not good in dates conversion. 😃
It probably helps to explain where those numbers came from.
SAS stores datetime values in the number of seconds since 1960 and Excel uses days since 1900 with the time part being stored as a fraction of a day.
To get the days fixed you need to subtract the difference in days between 1960 and 1900 (note that Excel misses one leap day and starts counting from 1 instead of 0). To convert to datetime you need to multiply by the number of seconds in a day to get seconds instead of days.
Using DATE and TIME literals will make the code clearer.
sas_date = int(excel_date - ('01JAN1960'd-'01JAN1900'd+2));
sas_datetime = (excel_date - ('01JAN1960'd-'01JAN1900'd+2)) * '24:00't;
1) There are a number ISO formats available. You might want to specify which one you want.
2) WHY can't you go back to the import step? The specific values as imported look like they have been imported incorrectly as the "date" component I get for those values is 1Jan1960 which usually indicates a datetime that is not correct, especially for the ranges of values you show.
3) You may have to show how the data was imported to get those values.
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.
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.