Published on :
Function CREATION_INTERNE

Creating custom functions for weekdays

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating a test dataset containing several dates. It then uses the PROC FCMP procedure to compile and store two custom functions in the `work.cat_function` catalog. The first function, `DayWeek`, returns the name of the weekday (in French or English) for a given date. The second function, `NumDayWeek`, returns the number of the weekday (where 1 = Monday). These functions are then tested and applied to the `test` dataset.
Data Analysis

Type : CREATION_INTERNE


The data used for testing is generated directly within the script via a DATA step using the DATALINES statement.

1 Code Block
DATA STEP Data
Explanation :
Creation of a temporary SAS table named 'test' containing a 'date' variable populated with raw data (datalines) to test the functions.
Copied!
1DATA test;
2\tinput date;
3\tinformat date date9.;
4\tformat date date9.;
5\tdatalines;
6\t\t31DEC2016
7\t\t05JAN2017
8\t\t05APR2017
9\t\t09JUL2017
10\t\t10NOV2017
11\t\t31DEC2017
12\t\t;
13RUN;
2 Code Block
PROC FCMP
Explanation :
Definition of the custom function 'DayWeek' that takes a date and a language code ('EN' or other for FR) and returns the day name. The function is compiled into the 'work.cat_function.test' package. It adjusts the WEEKDAY result so that Monday is day 1.
Copied!
1PROC FCMP outlib=work.cat_function.test;
2\tfunction DayWeek(DateDay,Language $) $;
3\t\t/* There is no test if DateDay is a real date.. */
4\t\tLg = Language;
5\t\tnd=WEEKDAY(DateDay);
6\t\tIF nd = 1
7\t\t\tTHEN
8\t\t\t\tnd = 7;
9\t\t\tELSE
10\t\t\t\tnd+-1;
11\t\tlength d $ 9;
12\t\tif Lg='EN' THEN DO;
13\t\t\tselect (nd);
14\t\t\t\twhen (1) d='monday';
15\t\t\t\twhen (2) d='tuesday';
16\t\t\t\twhen (3) d='wednesday';
17\t\t\t\twhen (4) d='thursday';
18\t\t\t\twhen (5) d='friday';
19\t\t\t\twhen (6) d='saturday';
20\t\t\t\twhen (7) d='sunday';
21\t\t\t\totherwise put 'Not a date !';
22\t\t\tend;
23\t\tend;
24\t\telse DO;
25\t\t\tselect (nd);
26\t\t\t\twhen (1) d='lundi';
27\t\t\t\twhen (2) d='mardi';
28\t\t\t\twhen (3) d='mercredi';
29\t\t\t\twhen (4) d='jeudi';
30\t\t\t\twhen (5) d='vendredi';
31\t\t\t\twhen (6) d='samedi';
32 \twhen (7) d='dimanche';
33\t\t\t\totherwise put 'Not a date !';
34\t\t\tend;
35\t\tend;
36\treturn(d);
37\tendsub;
38RUN;
3 Code Block
DATA STEP Data
Explanation :
Application of the 'DayWeek' function on the 'test' table. Creates two new columns 'da' (French) and 'da2' (English). Note: For this to work without global options, the SAS session must know the location of the CMPLIB functions, although the 'cmplib' option is not explicitly defined here, it is often required.
Copied!
1DATA test;
2\tset test;
3\tlength da da2 $ 9;
4\tformat da da2 $9.;
5\tda=DayWeek(date,'FR');
6\tda2=DayWeek(date,'EN');
7RUN;
4 Code Block
PROC FCMP
Explanation :
Definition of the second custom function 'NumDayWeek'. It returns the day number as a character string, with Monday = 1 and Sunday = 7.
Copied!
1PROC FCMP outlib=work.cat_function.test;
2\tfunction NumDayWeek(DateDay) $;
3\t\t/* There is no test if DateDay is a real date.. */
4\t\tnd=WEEKDAY(DateDay);
5\t\tIF nd = 1
6\t\t\tTHEN
7\t\t\t\tnd = 7;
8\t\t\tELSE
9\t\t\t\tnd+-1;
10\t\tlength d $ 1;
11\t\td=put(nd,1.);
12\treturn(d);
13\tendsub;
14RUN;
5 Code Block
DATA STEP Data
Explanation :
Application of the 'NumDayWeek' function on the 'test' table to create the 'Numday' column.
Copied!
1DATA test;
2\tset test;
3\tlength Numday $ 1;
4\tformat Numday $1.;
5\tNumday=NumDayWeek(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 : Author(s) : Nicolas DUPONT - Creation date : 14/04/2017