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!
data test;\n\tinput date;\n\tinformat date date9.;\n\tformat date date9.;\n\tdatalines;\n\t\t31DEC2016\n\t\t05JAN2017\n\t\t05APR2017\n\t\t09JUL2017\n\t\t10NOV2017\n\t\t31DEC2017\n\t\t;\nrun;
1
DATA 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;
13
RUN;
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!
proc fcmp outlib=work.cat_function.test;\n\tfunction DayWeek(DateDay,Language $) $;\n\t\t/* There is no test if DateDay is a real date.. */\n\t\tLg = Language;\n\t\tnd=WEEKDAY(DateDay);\n\t\tIF nd = 1 \n\t\t\tTHEN \n\t\t\t\tnd = 7; \n\t\t\tELSE \n\t\t\t\tnd+-1;\n\t\tlength d $ 9;\n\t\tif Lg='EN' then do;\n\t\t\tselect (nd);\n\t\t\t\twhen (1) d='monday';\n\t\t\t\twhen (2) d='tuesday';\n\t\t\t\twhen (3) d='wednesday';\n\t\t\t\twhen (4) d='thursday';\n\t\t\t\twhen (5) d='friday';\n\t\t\t\twhen (6) d='saturday';\n\t\t\t\twhen (7) d='sunday';\n\t\t\t\totherwise put 'Not a date !';\n\t\t\tend;\n\t\tend;\n\t\telse do;\n\t\t\tselect (nd);\n\t\t\t\twhen (1) d='lundi';\n\t\t\t\twhen (2) d='mardi';\n\t\t\t\twhen (3) d='mercredi';\n\t\t\t\twhen (4) d='jeudi';\n\t\t\t\twhen (5) d='vendredi';\n\t\t\t\twhen (6) d='samedi';\n \twhen (7) d='dimanche';\n\t\t\t\totherwise put 'Not a date !';\n\t\t\tend;\n\t\tend;\n\treturn(d);\n\tendsub;\nrun;
1
PROC 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'THENDO;
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;
38
RUN;
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!
data test;\n\tset test;\n\tlength da da2 $ 9;\n\tformat da da2 $9.;\n\tda=DayWeek(date,'FR');\n\tda2=DayWeek(date,'EN');\nrun;
1
DATA test;
2
\tset test;
3
\tlength da da2 $ 9;
4
\tformat da da2 $9.;
5
\tda=DayWeek(date,'FR');
6
\tda2=DayWeek(date,'EN');
7
RUN;
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!
proc fcmp outlib=work.cat_function.test;\n\tfunction NumDayWeek(DateDay) $;\n\t\t/* There is no test if DateDay is a real date.. */\n\t\tnd=WEEKDAY(DateDay);\n\t\tIF nd = 1 \n\t\t\tTHEN \n\t\t\t\tnd = 7; \n\t\t\tELSE \n\t\t\t\tnd+-1;\n\t\tlength d $ 1;\n\t\td=put(nd,1.);\n\treturn(d);\n\tendsub;\nrun;
1
PROC 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;
14
RUN;
5 Code Block
DATA STEP Data
Explanation : Application of the 'NumDayWeek' function on the 'test' table to create the 'Numday' column.
Copied!
data test;\n\tset test;\n\tlength Numday $ 1;\n\tformat Numday $1.;\n\tNumday=NumDayWeek(date);\nrun;
1
DATA test;
2
\tset test;
3
\tlength Numday $ 1;
4
\tformat Numday $1.;
5
\tNumday=NumDayWeek(date);
6
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.
Copyright Info : Author(s) : Nicolas DUPONT - Creation date : 14/04/2017
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.