Published on :
Data Processing CREATION_INTERNE

Interval Alignment and Date Functions

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script explores in detail the `INTNX` and `INTCK` functions, essential for manipulating and calculating dates and intervals. It shows how `INTNX` allows advancing a date by a certain interval using various alignment options (beginning, middle, end, same day of the month/year). The script also addresses how `INTNX` handles date advancements that could result in 'illegal' dates (e.g., February 29th in a non-leap year). The `INTCK` function is illustrated for counting the number of intervals (here, weeks) between two dates, showing the differences between 'continuous' and 'discrete' counting methods.
Data Analysis

Type : CREATION_INTERNE


All data used in this script is generated internally via DATA steps. The script creates datasets (`ExamSchedule`, `check`) or uses `DATA _NULL_` for direct demonstrations, relying on literal dates and loops to simulate time series or specific scenarios.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a dataset named `ExamSchedule`. It generates a series of dates (`visdt`) from June 1st to June 10th, 2007. For each date, the `INTNX` function is used to calculate the next month with different alignment options: default (beginning of the month), 'beginning' (beginning of the month), 'middle' (middle of the month), 'end' (end of the month), and 'same' (same day of the month). The resulting dates are stored in new variables (`next_d`, `next_b`, `next_m`, `next_e`, `next_s`) and formatted for better readability.
Copied!
1title1 '3.4.3 Alignment Options';
2 
3DATA ExamSchedule;
4 DO visdt = '01jun2007'd to '10jun2007'd;
5 next_d = intnx('month',visdt,1);
6 next_b = intnx('month',visdt,1,'beginning');
7 next_m = intnx('month',visdt,1,'middle');
8 next_e = intnx('month',visdt,1,'end');
9 next_s = intnx('month',visdt,1,'same');
10 OUTPUT;
11 END;
12 FORMAT visdt next: date7.;
13 RUN;
2 Code Block
PROC PRINT
Explanation :
This `PROC PRINT` procedure displays the content of the `ExamSchedule` dataset created previously. This allows visualizing and comparing the results of the different `INTNX` function alignment options applied to dates.
Copied!
1PROC PRINT DATA=examschedule;
2 RUN;
3 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` block is used for a direct demonstration without creating a permanent dataset. It illustrates the effect of the 'middle' ('m') option of the `INTNX` function on different dates. It shows how `INTNX` calculates the middle of the next month, taking into account leap years or the variable number of days per month, and displays the results in the SAS log.
Copied!
1DATA _null_;
2 mfeb= intnx('month','01jan2009'd, 1, 'm');
3 leap= intnx('month','01jan2008'd, 1, 'm');
4 mapr= intnx('month','01jan2008'd, 3, 'm');
5 mmay= intnx('month','01jan2008'd, 4, 'm');
6 put mfeb= leap= mapr= mmay=;
7 FORMAT mfeb leap mapr mmay date9.;
8 RUN;
4 Code Block
DATA STEP
Explanation :
This second `DATA _NULL_` block demonstrates a specific case of `INTNX`: advancing to 'illegal' dates. It calculates the next year from February 29, 2008 (leap year) and the next month from May 31, 2008 (31-day month), using the 'same' ('s') option. The results, which show how SAS handles these advancements (e.g., February 28th for the next year if not a leap year), are displayed in the log.
Copied!
1DATA _null_;
2 leap = intnx('year', '29feb2008'd, 1, 's');
3 short= intnx('month','31may2008'd, 1, 's');
4 put leap= short=;
5 FORMAT leap short date9.;
6 RUN;
5 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a dataset named `check`. It initializes a fixed start date (`start`) to September 14, 2011. Then, it loops through the dates, from the start date to the 'same date' of the next month, calculated with `INTNX`. In each iteration, the `INTCK` function is used to count the number of 'weeks' between the start date and the current end date, using the default, 'continuous' ('c'), and 'discrete' ('d') options. The `check` dataset records these calculations.
Copied!
1* Alignment for INTCK;
2DATA check;
3 start = '14sep2011'd; * the 14th was a Wednesday;
4 DO END = start to intnx('month',start,1,'s');
5 weeks = intck('weeks',start,END);
6 weeksc= intck('weeks',start,END,'c');
7 weeksd= intck('weeks',start,END,'d');
8 OUTPUT check;
9 END;
10FORMAT start END date9.;
11RUN;
6 Code Block
PROC PRINT
Explanation :
This final `PROC PRINT` procedure displays the content of the `check` dataset. It allows examining how the different counting options ('default', 'continuous', 'discrete') of the `INTCK` function affect the calculation of the number of weeks between two given dates.
Copied!
1PROC PRINT DATA=check;
2RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.