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

I am subtracting two dates in sas using the YRdiff function but I am not getting same result as excel using the yearfrac function. My expected result in the expect column. Also, intck function gives me the integer number.

 

I need help. Thanks in advance.

 

data test;

input date1 date2 expected_result;

informat date1 date2 yymmdd10.;

format date1 date2 yymmdd10.;

cards;

 

2018/10/31 2022/07/15 3.708333333

2018/10/31 2020/04/15 1.458333333

2018/10/31 2020/04/15 1.458333333

2018/10/31 2021/01/15 2.208333333

2018/10/31 2019/09/16 0.877777778

2018/10/31 2022/04/18 3.466666667

2018/10/31 2020/04/15 1.458333333

2018/10/31 2019/02/19 0.302777778

2018/10/31 2019/01/15 0.208333333

2018/10/31 2021/01/15 2.208333333

2018/10/31 2021/05/17 2.547222222

2018/10/31 2023/08/15 4.791666667

2018/10/31 2021/04/15 2.458333333

2018/10/31 2020/04/15 1.458333333

2018/10/31 2022/07/15 3.708333333

2018/10/31 2021/04/15 2.458333333

2018/10/31 2019/02/19 0.302777778

2018/10/31 2023/08/15 4.791666667

2018/10/31 2019/09/16 0.877777778

2018/10/31 2021/05/17 2.547222222

2018/10/31 2022/04/18 3.466666667

2018/10/31 2019/01/15 0.208333333

;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

You can get your expected result by setting the third parameter to the yrdif function (basis) to '30/360'. This parameter is used to control how the actual day count is calculated - the default in the Excel yrfrac formula appears to be '30/360' whereas in SAS the default is 'AGE'. The two methods yield slightly different results hence the need to explicitly set the basis. You can find further details of the allowable values for basis here -> http://support.sas.com/documentation/cdl//en/lefunctionsref/69762/HTML/default/viewer.htm#p1pmmr2dte...

 

Here's the sample code to give you the output you seek

 

data out;
	set test;
	mydif=yrdif(date1,date2,'30/360');
run;

View solution in original post

3 REPLIES 3
ballardw
Super User

If you meant the YRDIF (one F) function you should show which basis, the third parameter, you used as well as the basis used in Excel.. There are several options and you can get quite different results. The default for YRDIF without a basis is AGE which does not have a corresponding basis for Excel Yearfrac function.

 

Also make sure that your Excel values have no fraction date components. It is possible to have a fraction of a day in Excel that does not display because a date only display setting is used.

ChrisBrooks
Ammonite | Level 13

You can get your expected result by setting the third parameter to the yrdif function (basis) to '30/360'. This parameter is used to control how the actual day count is calculated - the default in the Excel yrfrac formula appears to be '30/360' whereas in SAS the default is 'AGE'. The two methods yield slightly different results hence the need to explicitly set the basis. You can find further details of the allowable values for basis here -> http://support.sas.com/documentation/cdl//en/lefunctionsref/69762/HTML/default/viewer.htm#p1pmmr2dte...

 

Here's the sample code to give you the output you seek

 

data out;
	set test;
	mydif=yrdif(date1,date2,'30/360');
run;
novinosrin
Tourmaline | Level 20

Slick & classic answer sir!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1746 views
  • 3 likes
  • 4 in conversation