![]() | ![]() | ![]() | ![]() | ![]() |
The sample code on the Full Code tab illustrates how to shift a date forward or backward by some number of months. Maintain the same day of the month wherever possible and adjust for months of different lengths.
Note: The SAMEDAY argument to the INTNX function is new in SAS® 9.1. Using SAMEDAY as the alignment argument in INTNX function will specify that the date returned is aligned to the same calendar date with the corresponding interval increment. In this sample, using SAMEDAY maintains the exact day of the month for the newly adjusted date. See Sample 1.
If you want to allow shifted dates with fewer days than the starting month to 'roll over' to the next month, see Sample 2.
For releases prior to SAS 9.1, see Sample 25960.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample 1 -- Rolling dates forwards and backwards by month using the SAMEDAY */
/* parameter new in SAS 9.1. Do NOT 'roll over' to next month if */
/* the adjusted month has fewer days than the day value of the */
/* starting month. */
data _null_;
/* Test with non-leap year */
do date='27MAR2003'd to '01APR2003'd;
lastmonth= intnx('month',date,-1,'sameday');
put (date lastmonth)(=worddate.);
end;
/* Test with leap year */
put;
do date='27MAR2004'd to '01APR2004'd;
lastmonth=intnx('month',date,-1,'sameday');
put (date lastmonth)(=worddate.);
end;
put;
/* Test future dates */
do date='27JAN2004'd to '01FEB2004'd;
nextmonth=intnx('month',date,1,'sameday');
put (date nextmonth)(=worddate.);
end;
run;
/* Sample 2 -- Rolling dates forwards and backwards by month using the SAMEDAY */
/* parameter new in SAS 9.1 while allowing shifted months with */
/* fewer days to 'roll over' to the next month. */
/* */
/* The default alignment of the INTNX function is the beginning of the shift */
/* period, the first of the month in this case. If the resulting date is in a */
/* month with less than 31 days, adjust the appropriate number of days into */
/* the next month. */
data _null_;
/* Test with non-leap year */
do date='27MAR2003'd to '01APR2003'd;
lastmonth=date - intnx('month',date,0)+intnx('month',date,-1);
put (date lastmonth)(=worddate.);
end;
/* Test with leap year */
put;
do date='27MAR2004'd to '01APR2004'd;
lastmonth=date - intnx('month',date,0)+intnx('month',date,-1);
put (date lastmonth)(=worddate.);
end;
put;
/* Test future dates */
do date='27JAN2004'd to '01FEB2004'd;
nextmonth=date - intnx('month',date,0)+intnx('month',date,1);
put (date nextmonth)(=worddate.);
end;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT to SAS log: Sample 1 -- SAS 9.1 using SAMEDATE parameter
date=March 27, 2003 lastmonth=February 27, 2003
date=March 28, 2003 lastmonth=February 28, 2003
date=March 29, 2003 lastmonth=February 28, 2003
date=March 30, 2003 lastmonth=February 28, 2003
date=March 31, 2003 lastmonth=February 28, 2003
date=April 1, 2003 lastmonth=March 1, 2003
date=March 27, 2004 lastmonth=February 27, 2004
date=March 28, 2004 lastmonth=February 28, 2004
date=March 29, 2004 lastmonth=February 29, 2004
date=March 30, 2004 lastmonth=February 29, 2004
date=March 31, 2004 lastmonth=February 29, 2004
date=April 1, 2004 lastmonth=March 1, 2004
date=January 27, 2004 nextmonth=February 27, 2004
date=January 28, 2004 nextmonth=February 28, 2004
date=January 29, 2004 nextmonth=February 29, 2004
date=January 30, 2004 nextmonth=February 29, 2004
date=January 31, 2004 nextmonth=February 29, 2004
date=February 1, 2004 nextmonth=March 1, 2004
OUTPUT to SAS log: Sample 2 -- Allow 'roll over' to next month
date=March 27, 2003 lastmonth=February 27, 2003
date=March 28, 2003 lastmonth=February 28, 2003
date=March 29, 2003 lastmonth=March 1, 2003
date=March 30, 2003 lastmonth=March 2, 2003
date=March 31, 2003 lastmonth=March 3, 2003
date=April 1, 2003 lastmonth=March 1, 2003
date=March 27, 2004 lastmonth=February 27, 2004
date=March 28, 2004 lastmonth=February 28, 2004
date=March 29, 2004 lastmonth=February 29, 2004
date=March 30, 2004 lastmonth=March 1, 2004
date=March 31, 2004 lastmonth=March 2, 2004
date=April 1, 2004 lastmonth=March 1, 2004
date=January 27, 2004 nextmonth=February 27, 2004
date=January 28, 2004 nextmonth=February 28, 2004
date=January 29, 2004 nextmonth=February 29, 2004
date=January 30, 2004 nextmonth=March 1, 2004
date=January 31, 2004 nextmonth=March 2, 2004
date=February 1, 2004 nextmonth=March 1, 2004
Shift a date forward or backward by some number of months while keeping the same day of the month.
Type: Sample Topic: SAS Reference ==> DATA Step
SAS Reference ==> Functions ==> Date and Time
Date Modified: 2009-04-01 10:35:11 Date Created: 2004-09-30 14:09:10
Operating System and Release Information
Product Family Product Host SAS Release Starting Ending SAS System Base SAS All 9.1 TS1M0 n/a




