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

Hi.  I'm wondering what would be the best way to determine the number of days between two SAS date variables and return it as a number variable?

 

Any suggests would be very helpful.  

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Use the INTCK Function like this

 

data _null_;
   date1='01jan2019'd;
   date2=today();
   days=intck('day', date1, date2);
   put days=;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User
Depends a bit on your data structure, but you can just subtract dates to get durations.
buechler66
Barite | Level 11

What do you mean depending on data structure?  As an example I'd be looking at dates stored like:

03-FEB-2019 minus 01-FEB-2019 to return 2 as the number of days.

 

Does that make sense?  

Reeza
Super User

It depends on if your dates are SAS dates with a numeric format and not character. 

It also depends on whether both dates are in the same line or not - if you need to look behind or forward you would use different techniques. At the end of the day, a difference calculation is all you need. 

 


@buechler66 wrote:

What do you mean depending on data structure?  As an example I'd be looking at dates stored like:

03-FEB-2019 minus 01-FEB-2019 to return 2 as the number of days.

 

Does that make sense?  


 

 

PeterClemmensen
Tourmaline | Level 20

Use the INTCK Function like this

 

data _null_;
   date1='01jan2019'd;
   date2=today();
   days=intck('day', date1, date2);
   put days=;
run;
buechler66
Barite | Level 11
Thanks so much for the example. I appreciate you taking the time.
ChanceTGardener
SAS Employee

For number of days between two SAS dates, you can just subtract one from the other. Behind the scenes, a SAS date is just a number (# of days since Jan. 1, 1960). 

 

data x;
 date1='01JAN2019'd;
 date2='04FEB2019'd;
 days_between=date2 - date1;
* format date1 date2 mmddyy10.;
run;

proc print data=x; run;

If you want the number of weeks, months, quarters, etc. between two SAS dates, check out the intck() function. 

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
  • 6 replies
  • 37737 views
  • 4 likes
  • 4 in conversation