Barite | Level 11

## To find the difference of two dates

Hello team,

I need to find out the difference between two variables: a dateofservice and two years back from that date of service. In fact, I want to use it in a where clause and say where lookbackperiod is less than or equal to two years. Years can be expressed in term of days as well.

How can I do this in proc sql/ data steps?

Thanks,

blue & blue

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: To find the difference of two dates

This is a great resource by Rick Wicklin on doing what you need:

https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html
10 REPLIES 10
SAS Employee

## Re: To find the difference of two dates

Look into the YRDIF() function and then incorporate it into your where clause.

https://go.documentation.sas.com/doc/en/vdmmlcdc/1.0/lefunctionsref/p1pmmr2dtec32an1vbsqmm3abil5.htm

SAS Super FREQ

## Re: To find the difference of two dates

This is a great resource by Rick Wicklin on doing what you need:

https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html
Super User

## Re: To find the difference of two dates

More details may help.

The general function to modify a date value a given amount is INTNX but that requires a base date to calculate from

data example;
datevalue = '15Mar2022'd;
newdate1 = intnx('year',datevalue, -2,'E');
newdate2 = intnx('year',datevalue, -2,'S');
newdate3 = intnx('year',datevalue, -2,'B');
format datevalue new: date9.;
run;

The first parameter is an interval, typical are year, month, week, day though others a acceptable, then the date value to calculate from, then the number of intervals, optional parameters return the End, Beginning or Same date of the interval.

This function can be used with WHERE as long as the date value is available, either a literal date or variable.

Barite | Level 11

## Re: To find the difference of two dates

Hello teammate,

My datevalue is a variable, which its content are different dates as 11/12/2019 or 0/05/2020, etc, I need to pull dates that are 2 years far from 12,31,2021.

Regards,

Blue&Blue

Blue Blue
Diamond | Level 26

## Re: To find the difference of two dates

I still don't understand what you are trying to do.

Please provide example data, AND the desired result.

--
Paige Miller
SAS Super FREQ

## Re: To find the difference of two dates

What is 'far'? Is it two years ahead or two years behind? Here's an example you can work with and tell us what you want to do.

data have;
input id date :mmddyy10.;
format date mmddyy10.;
datalines;
1 11/12/2019
2 05/05/2020
3 12/31/2019
4 12/31/2018
5 12/31/2020
6 03/04/2022
7 04/05/1980
8 12/31/2022
;
run;

I ended up using the YRDIF function to compute the actual days between the two.

data want;
set have;
years_from = yrdif("31DEC2021"d, date, 'actual');
run;

You can then use FLOOR or CEIL functions to get what you want since we don't know.

data want;
set have;
years_from = yrdif("31DEC2021"d, date, 'actual');
years_ceil = ceil(years_from);
years_floor = floor(years_from);
run;

It's really important that we know what you want. Please always provide example data like I have above. Also provide a data set showing exactly what you want. It allows people to answer the questions and increases the likelihood of people answering your questions.

Barite | Level 11

## Re: To find the difference of two dates

Hello,

Bringing Example made it more clear. Thanks for it.

blue&blue

Blue Blue
Barite | Level 11

Hello,

thanks,

blue&blue

Blue Blue
SAS Employee

## Re: To find the difference of two dates

I suggest you research them yourself first, then if there's something you don't understand or are having trouble applying, ask in here.

Barite | Level 11

## Re: To find the difference of two dates

Tom,

thanks for this. This helped me in doing my project.
blue&blue

Blue Blue
Discussion stats
• 10 replies
• 705 views
• 5 likes
• 5 in conversation