count between dates in sas

Reply
Occasional Contributor GS2
Occasional Contributor
Posts: 16

count between dates in sas

I have my data imported from excel into SAS, how do I put the dates from 2 different columns into SAS format and then calculate the days or years between the 2 dates. Thanks

 

Using version 9.4

SAS Employee
Posts: 30

Re: count between dates in sas

The data should automatically be converted into SAS date values when it is imported to SAS. That means your data will be converted to an integer number which is "number of days since Jan 1, 1960".

 

To find the number of days between two dates, simply subtract the two fields. For example: if I have a date1 column and a date2 column, I could calculate the difference as follows:

DATA work.test;
     SET work.dates;
     diff = date1-date2;
RUN;

This assumes that a data set called "work.dates" is where our excel file was imported. Also, this assumes date1 is BEFORE date2, but if you have the opposite scenario, flip them!

 

 

When you want to print these dates out, you may need to apply a SAS format to make them human readable. Something like

FORMAT diff MMDDYY10.;

is a great starting point.

 

Esteemed Advisor
Posts: 5,529

Re: count between dates in sas

SAS has date functions that free you from the internal representation of dates and provide reliable interval calculations. The number of day or year changes between dates are given by:

 

nDays = intck("DAY", firstDate, secondDate);

nYears = intck("YEAR", firstDate, secondDate);

 

Note: if firstDate is 20DEC2017 and secondDate is 5JAN2018, nYear will be 1 because the year has changed once between the two dates. The number of whole years between two dates is given by

 

wYears = intck("YEAR", firstDate, secondDate, "CONTINUOUS");

 

and that would be 0.

 

Check the documentation for functions INTCK and INTNX for more details. It is well worth the trouble.

PG
Ask a Question
Discussion stats
  • 2 replies
  • 111 views
  • 0 likes
  • 3 in conversation