BookmarkSubscribeRSS Feed
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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

2 REPLIES 2
GinaRepole
SAS Employee

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.

 

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 323 views
  • 0 likes
  • 3 in conversation