As the first step in the decommissioning of the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

INTNX Function examples

From sasCommunity
Jump to: navigation, search

This article is about the INTNX() function. I believe that the best way to understand how it works is to see some easy examples. Thus, in this article you will find some. Full description of the INTNX function you may find in your SAS Help.

According to SAS Help this function Increments a date, time, or datetime value by a given interval or intervals, and returns a date, time, or datetime value. (c) SAS Help.

Example1. Some simple examples of using INTNX function:

format day week month_ year date9.;
day=intnx('day', '01FEB2010'd, 7); /* +7 days */
week=intnx('week', '01FEB2010'd, 1); /* 01 of Feb 2010 is Monday*/
month_=intnx('month', '01FEB2010'd, 2); /* +2 month */
year=intnx('year', '01FEB2010'd, 1); /* +1 year */

The result values will be:

  • day = 08FEB2010 (+7 days)
  • week = 07FEB2010 (next Sunday)
  • month_ = 01APR2010 (next 1st day of 2nd next month)
  • year = 01JAN2011 (next 1st day of next year)

Example2. Multiplying and shifting intervals.

You may change the first argument (interval) by adding digits as in the example below.

format day week year date9.;
day=intnx('day2', '01FEB2010'd, 2); /* two of 2-days intervals */
week=intnx('week1.3', '01FEB2010'd, 1); /* 01 of Feb 2010 is Sunday. 3rd day of the week is Tuesday */
year=intnx('year1.3', '01FEB2010'd, 1); /* next year | third month | 1st day*/

The result values will be:

  • day = 05FEB2010 (+2 days twice)
  • week = 02FEB2010 (Next Tuesday)
  • year = 01MAR2011 (Next Year's third month (March) 1st day)

You are free to combine intervals by multiplying and shifting it. However, please be careful when contructing complex interval specifications.

Example3. Datetime and time formats.

The INTNX function may cope with datetime, time and date formats. The above examples are based on date format. The example below uses datetime and time formats.

format seconds minutes hours days weeks datetime20.;
format t_seconds t_minutes t_hours time9.;
seconds=intnx('second', '01FEB2010:00:00:00'dt, 1); 
minutes=intnx('minute', '01FEB2010:00:00:00'dt, 1); 
hours=intnx('hour', '01FEB2010:00:00:00'dt, 1); 
days=intnx('dtDay', '01FEB2010:00:00:00'dt, 1); 
weeks=intnx('dtWeek', '01FEB2010:00:00:00'dt, 1);
t_seconds=intnx('second', '00:00:00't, 1); 
t_minutes=intnx('minute', '00:00:00't, 1); 
t_hours=intnx('hour', '00:00:00't, 1);

The result values will be:

  • seconds= 01FEB2010:00:00:01 (next second)
  • minutes= 01FEB2010:00:01:00 (next minute)
  • hours= 01FEB2010:01:00:00 (next hour)
  • days= 02FEB2010:00:00:00 (next day)
  • weeks= 07FEB2010:00:00:00 (next Sunday)
  • t_seconds = 00:00:01 (next second)
  • t_minutes = 00:01:00 (next minute)
  • t_hours = 01:00:00 (next hour)

and so on.

Example4. Alignment within the interval.

format beginning middle end sameday date9.;
beginning=intnx('month', '05FEB2010'd, 1, 'b'); 
middle=intnx('month', '05FEB2010'd, 1, 'm');  
end=intnx('month', '05FEB2010'd, 1, 'e'); 
sameday=intnx('month', '05FEB2010'd, 1, 's');

The result values will be:

  • beginning = 01MAR2010 (1st day of the next month)
  • middle= 16MAR2010 (middle day (16 of 31) of the next month)
  • end= 31MAR2010 (last day (31) of the next month)
  • sameday = 05MAR2010 (the same day (5) of the next month)

You might find it helpful to use the INTCK function along with the INTNX.

Good luck! --Victor Popovich 10:49, 20 January 2010 (UTC)