Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- How to convert these numbers to ISO date??

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 12:37 PM - edited 05-23-2017 12:43 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:12 PM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 12:59 PM

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.

Providing a default format for the datetime value makes the value meaningful when reported.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:05 PM

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

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

Solution

05-23-2017
01:38 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:12 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:40 PM - edited 05-23-2017 01:49 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:56 PM - edited 05-23-2017 02:04 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 02:27 PM

Great thank you Tom for detailed explanation... it helped a lot..

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:19 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2017 01:46 PM

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

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