Sample 45239: Create a function to calculate the weekday starting with Monday instead of Sunday
Use PROC FCMP to create a function that calculates the weekday starting with Monday instead of Sunday. User-defined functions and subroutines created by PROC FCMP can be used in the DATA Step beginning in SAS 9.2.
Sample based on an example by Gerhard Svolba, SAS Austria. Read more about books authored by Gerhard Svolba on his
SAS Press Author page.
Additional Documentation
For more information on PROC FCMP click on the link below to go to a
2007 SAS Global Forum paper about the procedure.
User-Written DATA Step Functions
Click on the link below to go to the SAS 9.2 documentation about PROC FCMP.
SAS 9.2 PROC FCMP Documentation
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Sample data */
data garage;
input dateofsrvce : mmddyy10. type: $20.;
datalines;
11/28/2011 oilchange
12/4/2011 30Kmile
12/5/2011 tires
12/13/2011 oilchange
12/15/2011 oilchange
12/15/2011 tires
12/18/2011 30Kmile
12/18/2011 oilchange
12/20/2011 tires
;
run;
/* Create a function using boolean logic with the WEEKDAY function
to adjust the start of the week from Sunday to Monday */
proc fcmp outlib = work.functions.samples;
function sas_myweekday(mydate);
return(weekday(mydate)-1+(weekday(mydate)=1)*7);
endsub;
run;
options cmplib=work.functions;
/* Use the function created in PROC FCMP to create MYWEEKDAY variable */
data garage2;
set garage;
datefulltext = dateofsrvce;
myweekday = sas_myweekday(dateofsrvce);
format dateofsrvce mmddyy10. datefulltext weekdatx.;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs dateofsrvce type datefulltext myweekday
1 11/28/2011 oilchange Monday, 28 November 2011 1
2 12/04/2011 30Kmile Sunday, 4 December 2011 7
3 12/05/2011 tires Monday, 5 December 2011 1
4 12/13/2011 oilchange Tuesday, 13 December 2011 2
5 12/15/2011 oilchange Thursday, 15 December 2011 4
6 12/15/2011 tires Thursday, 15 December 2011 4
7 12/18/2011 30Kmile Sunday, 18 December 2011 7
8 12/18/2011 oilchange Sunday, 18 December 2011 7
9 12/20/2011 tires Tuesday, 20 December 2011 2
Use PROC FCMP to create a function that calculates the weekday starting on Monday instead of Sunday. User-defined functions and subroutines created by PROC FCMP can be used in the DATA Step beginning in SAS 9.2.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> FCMP
|
Date Modified: | 2012-04-03 10:57:03 |
Date Created: | 2011-12-29 08:28:33 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9.2 TS1M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |
Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |
Microsoft® Windows® for x64 | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |
Microsoft Windows XP Professional | 9.2 TS1M0 | |
Windows Vista | 9.2 TS1M0 | |
Windows Vista for x64 | 9.2 TS1M0 | |
64-bit Enabled AIX | 9.2 TS1M0 | |
64-bit Enabled HP-UX | 9.2 TS1M0 | |
64-bit Enabled Solaris | 9.2 TS1M0 | |
HP-UX IPF | 9.2 TS1M0 | |
Linux | 9.2 TS1M0 | |
Linux for x64 | 9.2 TS1M0 | |
OpenVMS on HP Integrity | 9.2 TS1M0 | |
Solaris for x64 | 9.2 TS1M0 | |