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;
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;
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.
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;
Slick & classic answer sir!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.