How to convert these numbers to ISO date??

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to convert these numbers to ISO date??

[ Edited ]

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.


Accepted Solutions
Solution
‎05-23-2017 01:38 PM
Community Manager
Posts: 2,761

Re: How to convert these numbers to ISO date??

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

View solution in original post


All Replies
Valued Guide
Posts: 2,175

Re: How to convert these numbers to ISO date??

If you use a data step loader, I would reccomend following the informat with a & symbol which avoids the embedded blank becoming the terminator of the string.
Providing a default format for the datetime value makes the value meaningful when reported.
Occasional Contributor
Posts: 5

Re: How to convert these numbers to ISO date??

Importing already done, I can't change anything in importing now. Smiley Sad

 

I have to change these numbers into ISO dates using SAS programming only

Solution
‎05-23-2017 01:38 PM
Community Manager
Posts: 2,761

Re: How to convert these numbers to ISO date??

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

Occasional Contributor
Posts: 5

Re: How to convert these numbers to ISO date??

[ Edited ]

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. =)

Super User
Super User
Posts: 6,500

Re: How to convert these numbers to ISO date??

[ Edited ]

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;

 

Occasional Contributor
Posts: 5

Re: How to convert these numbers to ISO date??

Great thank you Tom for detailed explanation... it helped a lot..
Super User
Posts: 10,500

Re: How to convert these numbers to ISO date??

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.

 

 

Occasional Contributor
Posts: 5

Re: How to convert these numbers to ISO date??

1. this is the format I need 2014-11-11T04:41:32
anyway I got it with Chris suggestion..
2. Its imported by other team and we can't change it since its raw dataset
3. Its just regular proc import step. formats will messup with proc import.
anyway thank U for ur reply
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 160 views
  • 3 likes
  • 5 in conversation