BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GreekGod
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
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.
GreekGod
Fluorite | Level 6

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

 

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

ChrisHemedinger
Community Manager

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

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
GreekGod
Fluorite | Level 6

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. 😃

Tom
Super User Tom
Super User

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;

 

GreekGod
Fluorite | Level 6
Great thank you Tom for detailed explanation... it helped a lot..
ballardw
Super User

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.

 

 

GreekGod
Fluorite | Level 6
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

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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