76. DATE FUNCTIONS | YRDIF

                                    DATE FUNCTIONS | YRDIF


Function: YRDIF

Purpose: To return the difference in years between two dates.

Syntax: YRDIF(start-date, end-date, 'basis')

         -->start-date is a SAS date value.

         -->end-date is a SAS date value.

         -->basis is an argument that controls how SAS computes the result. The first value is used to specify the number of days in a month; the second value (after the slash) is used to specify the number of days in a year.

EXAMPLES OF BASIS:

-->'30/360' Uses 30-day months and 360-day years in the calculation.

-->'ACT/365' Uses the actual number of days between the two dates, but uses 365-day years, even if a leap year is in the interval.

-->'ACT/360' Uses the actual number of days between the two dates, but uses 360-day years.


CODE:

data period;

set dates;

Interval_month = intck('month',Date1,Date2);

Interval_year = intck('year',Date1,Date2);

Year_diff = yrdif(Date1,Date2,'actual');

Interval_qtr = intck('qtr',Date1,Date2);

Next_month = intnx('month',Date1,1);

Next_year = intnx('year',Date1,1);

Next_qtr = intnx('qtr',Date1,1);

Six_month = intnx('month',Date1,6);

format Next: Six_month date9.;

run;

title "Listing of Data Set PERIOD";

proc print data=period heading=h;

id date1 date2;

run;


HERE DATES DATASET IS IN THIS PAGE:https://sasall4you.blogspot.com/2025/01/70program-to-create-dates-dataset.html


Explanation:

Let me point out that the ID statement of PROC PRINT lists both DATE1 and DATE2 as ID variables. This allows the values to be repeated on the lower portion of the listing. The interval functions can be somewhat confusing. It helps to keep in mind that the INTCK function counts how many times you cross a boundary going from the start date to the end date.


OUTPUT:

         
                                                                      Listing of Data Set PERIOD


Date1 Date2 Interval_month Interval_year Year_diff Interval_qtr Next_month Next_year Next_qtr Six_month
01JAN1960 15JAN1960 0 0 0.03825 0 01FEB1960 01JAN1961 01APR1960 01JUL1960
02MAR1961 18FEB1962 11 1 0.96712 4 01APR1961 01JAN1962 01APR1961 01SEP1961
25DEC2000 03JAN2001 1 1 0.02461 1 01JAN2001 01JAN2001 01JAN2001 01JUN2001
01FEB2002 31MAR2002 1 0 0.15890 0 01MAR2002 01JAN2003 01APR2002 01AUG2002


TRY THIS AND COMMENT ...


-->PLEASE READ AND COMMENT THE BLOG...

--PLEASE FOLLOW THE BLOG FOR MORE UPDATES...

--FOLLOW US IN FACEBOOK SASALL4YOU AND JOIN ...

--JOIN US IN FACEBOOK AND TELEGRAM  CHANNEL FOR MORE UPDATES

   CLICK HERE: https://t.me/SasAll4You

Comments