BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Help...how can one calculate the number of days between a start date (contained within a field in a table) and "today" within EG?
5 REPLIES 5
ChrisHemedinger
Community Manager
Using the query builder, you can add a calculated column to your table. In the expression for the new column, use the DATDIF function. Example:

DATDIF(TABLE.STARTDATE, TODAY(), 'ACT/ACT')

where TABLE.STARTDATE is the column that contains the start date you refer to.

Chris
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
deleted_user
Not applicable
Thanks for the prompt reply...

I followed your example above, but unfortunately the new calcuated column did not return any values.

The format for the start date is DATEAMPM22.0

Could this be an issue?
Olivier
Pyrite | Level 9
Yes, the format indication explains it all : your date variable is not a "simple" date, but a datetime.
The DATEDIF function only deals with dates, not datetimes.

You should use the following formula :

TODAY() - DATEPART(yourDateColumn)

and this will return duration in days directly. Or, to derive from Chris' answer :

DATDIF(DATEPART(TABLE.STARTDATE), TODAY(), 'ACT/ACT')
deleted_user
Not applicable
Thanks again...it worked.

A related question...how to determine duration of days between a start and stop date, in DATEAMPM22.0 Format.

I have tried based on the 2 expressions above, but the values returned do not make sense. Far too large a number
Olivier
Pyrite | Level 9
Then again, it is a problem of dates and datetimes.
SAS dates are stored in number of days since January 1st, 1960. So substracting one date from another gives durations in days without needing functions.
SAS datetimes (and most date columns imported into SAS Guide from Oracle, DB2, Excel) are stored in number of seconds since midnight on January 1st, 1960. So substracting one datetime from another will give delays expressed in seconds (that should be the result that you currently get).
The DATEPART function is transform a datetime into a date, by removing the time part and computing the right number of days from 01/01/1960.

So your delay computations should look like :
DATEPART(column1) - DATEPART(column2)

The last question is to guess if a given column is date or datetime : check its format. If the format is something like DATETIME., DATEAMPM., then it is datetime. If the format is DATE., DDMMYY., MMYY., YEAR., then it is a SAS date.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2101 views
  • 0 likes
  • 3 in conversation