Published on :
Macro CREATION_INTERNE

Begin and End of Month Functions (BeginMonth, EndMonth)

This code is also available in: Deutsch Español Français
Awaiting validation
The provided SAS© script creates and tests two user-defined functions, BeginMonth and EndMonth, using PROC FCMP. The BeginMonth function calculates the first day of the month for a given date, while EndMonth calculates the last day. These functions leverage SAS©'s INTNX function. The script begins by generating a test dataset ('test') with various dates to illustrate their use. Subsequently, it applies these functions to the 'test' dataset to add new variables 'FirstDay' and 'LastDay', formatted as dates.
Data Analysis

Type : CREATION_INTERNE


The data used ('test') is created directly within the script via a DATA step and the DATALINES statement. There is no dependency on external data sources or default SAS libraries like SASHELP for input data.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a dataset named 'test'. It defines a 'date' variable, applies the INFORMAT (for reading) and FORMAT (for display) 'date9.' formats to it, and then populates it with date values specified via the DATALINES statement. This dataset serves as an example for testing the FCMP functions.
Copied!
1DATA test;
2 INPUT date;
3 informat date date9.;
4 FORMAT date date9.;
5 DATALINES;
6 31DEC2016
7 01JAN2017
8 15FEB2017
9 15FEB2016
10 08JAN2017
11 09JAN2017
12 25DEC2017
13 31DEC2017
14 01JAN2018
15 05JAN2017
16 05APR2017
17 09JUL2017
18 10NOV2017
19 31DEC2017
20 15MAR2017
21 ;
22RUN;
2 Code Block
PROC FCMP
Explanation :
This PROC FCMP procedure defines a user-defined function named 'BeginMonth'. The function takes a 'DateDay' argument (a numeric SAS date) and uses SAS's INTNX function to calculate the beginning of the month date ('b') corresponding to 'DateDay', with no offset ('0'). The function is stored in the 'work.cat_function.test' catalog and returns the first day of the month.
Copied!
1PROC FCMP outlib=work.cat_function.test;
2 function BeginMonth(DateDay);
3 res=intnx('month', DateDay, 0, 'b');
4 return(res);
5 endsub;
6RUN;
3 Code Block
PROC FCMP
Explanation :
This PROC FCMP procedure defines a user-defined function named 'EndMonth'. Similar to 'BeginMonth', it takes a 'DateDay' argument and uses INTNX to calculate the end of the month date ('e') corresponding to 'DateDay', with no offset. The function is also stored in the 'work.cat_function.test' catalog and returns the last day of the month.
Copied!
1PROC FCMP outlib=work.cat_function.test;
2 function EndMonth(DateDay);
3 res=intnx('month', DateDay, 0, 'e');
4 return(res);
5 endsub;
6RUN;
4 Code Block
DATA STEP
Explanation :
This DATA STEP block reads the existing 'test' dataset. It adds two new variables, 'FirstDay' and 'LastDay', and applies the 'date9.' format to them. Then, it calls the previously defined FCMP functions 'BeginMonth' and 'EndMonth', passing them the 'date' variable to calculate the first and last day of each month, respectively. The results are stored in the new variables of the 'test' dataset.
Copied!
1DATA test;
2 SET test;
3 FORMAT FirstDay lastDay DATE9.;
4 FirstDay=BeginMonth(date);
5 LastDay=EndMonth(date);
6RUN;
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.
Copyright Info : Creation date : 14/04/2017 (en) Last update : 14/04/2017 (en) Author(s) : Nicolas DUPONT