• Ekta Aggarwal

INTNX - Incrementing dates in SAS

When one is working with dates, sometimes they need to increase the date by a month or a day or a year etc.

For this SAS offers an in-built function INTNX which is used to increment the dates in SAS.


Syntax:

INTNX( <increment by what> , date, 'duration to be incremented', 'type')



Task: Let us try to increment a person's date of birth by 1 year.

DATA _NULL_ : It does not create any SAS dataset

PUT : Does not open any output window rather, prints the result in the log.

For this we have defined first parameter in INTNX as 'year' and third parameter as 1 - It defines that we have to increment the year by 1.

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('year',birthdt, 1);
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Note that the incremented date is 1st Jan 2008 instead of 18Oct 2008. This is because of the default fourth parameter i.e. 'beginning' or 'b':


Fourth parameter can take values:

  • 'beginning' or 'b': Returns day of incremented date as 01

  • 'end' or 'e' : Returns day of incremented date as last day of the month (28 or 29 or 30 or 31)

  • 'mid' or 'm' :Returns day of incremented date as 15

  • 'same' or 's' : Returns day of incremented date as same of original date.

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('year',birthdt, 1,'same');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Alternatively

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('year',birthdt, 1,'s');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Case: Fourth parameter is set to 'e' or 'end'

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('year',birthdt, 1,'end');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Similarly you can write 'e' instead of 'end'


Case: Fourth parameter is set to 'm' or 'mid'

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('year',birthdt, 1,'mid');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Case: Incrementing the date by 10 months.

For this we have specified our first parameter as 'month' and third as 10.

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('month',birthdt, 10,'same');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Case: Incrementing the date by 4 quarters (equivaluent to 1 year).

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('qtr',birthdt, 4,'same');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Case: Incrementing the date by 10 days.

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('day',birthdt, 10,'same');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Getting first date of the month


To get the first date of the month we do not want to increment the date thus our third parameter will be 0.

We want to go to the beginning of the month thus we have specified first parameter as 'month' and fourth parameter as 'beginning' or 'b'

DATA _NULL_;
FORMAT birthdt date9. incremented_date date9.;
birthdt = '18Oct2007'd;
incremented_date = intnx('month',birthdt, 0,'b');
PUT "Birthdate is: " birthdt ", Incremented date is: " incremented_date ;
RUN;

Tags: