Sample 25960: Shift a date by months while maintaining same day of the month (prior to SAS 9.1)
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: If you are using SAS 9.1 or higher, see
Sample 24749 where the INTNX function does the "shifting logic" for you via the SAMEDAY alignment argument.
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.
/********************************************************************************************/
/* The default alignment of the INTNX function is the beginning of the shift period, */
/* the first of the month in this case. If the day of the month of the start date is 1 */
/* through 28 or if the shifted date is in a month with 31 days, add the days back into */
/* the new date to get the 'exact' day value of the starting date. If the resulting date */
/* is in a month with fewer than 31 days, add the lesser of the day of the month or 29 */
/* to shift the date to the last day of the month. For example, if the starting date is */
/* January 31, rolling forward one month returns the last date in February, not March. */
/* */
/********************************************************************************************/
data one;
/* Read a date and the number of months to shift the date. A negative value for */
/* the N variable indicates shifting the date into the past while positive values */
/* yield future dates. */
input start mmddyy10. +1 n;
/* Use INTNX to roll forward or backwards N months. The default result will be the */
/* first day of the new month. */
shifted_date=intnx('month',start,n);
/* Use logic to determine how to calculate the exact day of the ending month, */
/* SHIFTED_DATE, based upon the number of days in SHIFTED_DATE and the starting */
/* day of the beginning month, START. */
dd=day(start)-1;
mm=month(shifted_date);
if dd>29 and mm ne 2 then do;
if mm in (1,3,5,7,8,10,12) then shifted_date +dd;
else if mm in (4,6,9,11) then shifted_date+(dd >< 29);
end;
else do;
if mod(year(shifted_date),4)=0 and year(shifted_date) ^= 2000
then shifted_date+(28 >< dd);
else shifted_date+(27 >< dd);
end;
format start shifted_date worddate.;
drop dd mm;
datalines;
05/31/2003 -3
12/30/2002 2
12/30/2003 2
08/03/2005 4
07/31/2005 2
01/01/2004 12
;
proc print;
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.
Obs start n shifted_date
1 May 31, 2003 -3 February 28, 2003
2 December 30, 2002 2 February 28, 2003
3 December 30, 2003 2 February 29, 2004
4 August 3, 2005 4 December 3, 2005
5 July 31, 2005 2 September 30, 2005
6 January 1, 2004 12 January 1, 2005
Shift a date forward or backward by some number of months while keeping the same day of the
month.
| Type: | Sample |
| Topic: | SAS Reference ==> Functions ==> Date and Time SAS Reference ==> DATA Step
|
| Date Modified: | 2008-06-04 12:48:17 |
| Date Created: | 2005-08-03 09:15:38 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |