Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
Handling Date Values [5-7]


INTNX Function
INTNX (pronounced as 'in-next') is a powerful function in SAS.

It allows you to find a future date that is x number of days from a given date.


For example, let's suppose a subject visited a clinic on Jan 1, 2019.

He/she was supposed to come back for a re-visit after 100 days.
Picture

​We can find the exact date for the clinic re-visit using the INTNX function.


Let's look at the code below:
data clinicvisit;
format visit1 visit2 date9.;

visit1 = '01JAN2019'd;
visit2 = intnx('day', visit1, 100);


run;

The VISIT1 variable is assigned the date constant of '01JAN2019'.

The VISIT2 calculates the date 100 days after VISIT1:
Picture

The date when the subject should come back for a re-visit is April 11, 2019. 


The INTNX function has three mandatory parameters:
​
Picture

(1) Interval Unit

The first parameter is the interval unit. In this example, we specify the interval unit as 'day'. 


(2) Start Date

The second parameter is the start date. In this example, it is the date of the subject's first visit (i.e. Jan 1, 2019).


(3) Increment

The third parameter is the increment. In this example, the increment is specified as 100.


The entire INTNX function tells SAS to find the date that is 100 days after the start date.


Adjusting the Interval

The INTNX function allows you to change the interval for your specific needs.

For example, let's assume the second visit should be exactly three weeks after the first visit.

This can also be calculated using the INTNX function.

Example
data clinicvisit;
format visit1 visit2 date9.;

visit1 = '01JAN2019'd;
visit2 = intnx('week', visit1, 3);

run;

In this example, we specified 'week' as the interval unit and a numeric 3 as the increment:
Picture

This tells SAS to calculate the date three weeks after the first visit (i.e. Jan 1, 2019).
Picture

The subject should come back on January 20 for a re-visit.


Wait a second. We have a problem here.

Jan 20 is not exactly three weeks after Jan 1:
Picture

When using the INTNX function, SAS, by default, aligns the date to be at the beginning of the interval.

In our example, the third 'week' after the week of Jan 1, 2019 is the week of Jan 20 to Jan 26.
Picture

By default, the INTNX function returns the first date of the interval, which is Jan 20, 2019.


Alignment

If you don't like the default alignment, you can specify the alignment as the optional fourth parameter.

Below are the alignment options:
  • Beginning (default)
  • Middle
  • End
  • Same

Let's go back to our example.

We can find the date that is exactly three weeks after the initial visit by specifying the keyword 'SAME' in the fourth parameter.

Example
data clinicvisit;
format visit1 visit2 date9.;

visit1 = '01JAN2019'd;
visit2 = intnx('week', visit1, 3, 'same');

run;

The keyword 'same' is specified as the fourth parameter of the INTNX function.

It returns the date exactly three weeks after the data of the initial visit, which is Jan 22, 2019:
Picture
Picture


Interval

The interval parameter in the INTNX function can also be specified as 'Month' and 'Year'.

This allows you to find the date that is x number of months or years after the given date.

Example
data clinicvisit;
format visit1 visit2 date9.;

visit1 = '01JAN2019'd;
visit2 = intnx('month', visit1, 9, 'end');

run;

In this example, the INTNX function calculates the date nine months after the initial clinic visit (i.e. Jan 1, 2019). 

The alignment 'end' can also be specified as the fourth parameter. 

This tells SAS to return the last day of the month, nine months after the initial visit:
Picture

Exercise
​
Copy and run the code from the yellow box below:

The CUST_LIST data set contains the subscription data from 300 paid TV subscribers.

It has three variables:
  • Customer no.
  • The subscription start date (SUBS_START_DATE)
  • Subscription length (e.g. 12, 24, 36, 48 months)

Let's assume today is January 1, 2019.

Create a data set that contains the subscribers whose subscription will end within the next three months.
Next

Need some help? 


HINT:
The subscription end date can be calculated using the INTNX function.


SOLUTION:

data cust_list2;
set cust_list;
format subs_end_date date9.;
subs_end_date = intnx('month', subs_start_date, month, 'same');

if subs_end_date >= '01JAN2019'd and subs_end_date < intnx('month', '01JAN2019'd, 3, 'same');
run;


Fill out my online form.
Already a member? Go to member's area.