BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
wbsjd
Obsidian | Level 7

Hello Everyone~

 

I am reading data from Excel to SAS.  In excel the requested Date is like the the below, a date time format,

wbsjd_0-1690395129196.png

However, after I Proc Import the Excel file to SAS, the time became a long ridiculous number. 

wbsjd_1-1690395248544.png

 

Can anybody help me to solve this problem? 

 

Thank you so much!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

What you get is the raw number Excel uses to store datetime values. To the left of the decimal point is the number of days since 1899-12-30 (which is day 0), to the right is the time as fraction of a day.

Add '30dec1899'd to the integer value, multiply the fractional part by 86400, and use both numbers as day and seconds argument for the DHMS function.

req_date = dhms(int(req_date)+'30dec1899'd,0,0,(req_date-int(req_date))*86400);
format req_date datetime20.;

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

What you get is the raw number Excel uses to store datetime values. To the left of the decimal point is the number of days since 1899-12-30 (which is day 0), to the right is the time as fraction of a day.

Add '30dec1899'd to the integer value, multiply the fractional part by 86400, and use both numbers as day and seconds argument for the DHMS function.

req_date = dhms(int(req_date)+'30dec1899'd,0,0,(req_date-int(req_date))*86400);
format req_date datetime20.;
wbsjd
Obsidian | Level 7
Thank you so much for the solution, it works perfectly
ballardw
Super User

You appear to have also done something else to make a character value from what should have been numeric.

Tom
Super User Tom
Super User

That is normally caused by having a column in the spreadsheet that mixes numeric and character cells.  When that happens SAS is forced to create a character variable (otherwise it could not store the character values).  So the numeric values get stored as digit strings representing the number.

 

To convert it back to a datetime value first use INPUT() to convert it to a number and then add in the SAS date for 30DEC1899 to adjust for differences in how the two systems number days.  You can use the DHMS() function then to convert it from days (with fractions of day) into seconds that SAS uses to store datetime values.

data want;
  set have;
  dt = dhms(input(requested_date,32.)+'30dec1899'd,0,0,0);
  format dt datetime19.;
run;
wbsjd
Obsidian | Level 7

Thank you so much for your solution and explanation. It's really helpful for me!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1522 views
  • 2 likes
  • 4 in conversation