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!
title1 '3.4.3 Alignment Options';
data ExamSchedule;
do visdt = '01jun2007'd to '10jun2007'd;
next_d = intnx('month',visdt,1);
next_b = intnx('month',visdt,1,'beginning');
next_m = intnx('month',visdt,1,'middle');
next_e = intnx('month',visdt,1,'end');
next_s = intnx('month',visdt,1,'same');
output;
end;
format visdt next: date7.;
run;
1
title1 '3.4.3 Alignment Options';
2
3
DATA 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!
proc print data=examschedule;
run;
1
PROC PRINTDATA=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!
data _null_;
mfeb= intnx('month','01jan2009'd, 1, 'm');
leap= intnx('month','01jan2008'd, 1, 'm');
mapr= intnx('month','01jan2008'd, 3, 'm');
mmay= intnx('month','01jan2008'd, 4, 'm');
put mfeb= leap= mapr= mmay=;
format mfeb leap mapr mmay date9.;
run;
1
DATA _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!
data _null_;
leap = intnx('year', '29feb2008'd, 1, 's');
short= intnx('month','31may2008'd, 1, 's');
put leap= short=;
format leap short date9.;
run;
1
DATA _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!
* Alignment for INTCK;
data check;
start = '14sep2011'd; * the 14th was a Wednesday;
do end = start to intnx('month',start,1,'s');
weeks = intck('weeks',start,end);
weeksc= intck('weeks',start,end,'c');
weeksd= intck('weeks',start,end,'d');
output check;
end;
format start end date9.;
run;
1
* Alignment for INTCK;
2
DATA check;
3
start = '14sep2011'd; * the 14th was a Wednesday;
4
DOEND = 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;
10
FORMAT start END date9.;
11
RUN;
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!
proc print data=check;
run;
1
PROC PRINTDATA=check;
2
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.