Sample 33838: Count for certain number of consecutive weekdays
The sample data consists of dates, some weekends and some weekdays. The goal is to determine how many days are consecutive, excluding weekends. A Friday and the following Monday are considered consecutive as well as days during the week that are consecutive.
The data is split into two data sets based on weekday versus weekend using the WEEKDAY function. The dates are tested for being consecutive and incrementing the variable COUNT by 1 when that's true. Otherwise, COUNT is reset to 1.
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 one;
input acct reportdate:date9. ;
format reportdate date9.;
datalines;
11111 08Oct2008
11111 09Oct2008
11111 10Oct2008
11111 13Oct2008
11111 16Oct2008
22222 08Oct2008
22222 10Oct2008
22222 14Oct2008
22222 01Oct2008
22222 05Oct2008
;
run;
proc sort;
by acct reportdate;
run;
/* put weekends into a data set and weekdays in another using */
/* WEEKDAY function */
data weekends weekdays;
set one;
if weekday(reportdate) in(1,7) then output weekends;
else output weekdays;
run;
/* Compute the lag of REPORTDATE. Increment COUNT by 1 if the */
/* current observation is a Monday and the previous is a Friday. */
/* Also increment COUNT by 1 if the previous observation and */
/* current are just 1 day apart. I */
data other;
set weekdays;
by acct;
lg=lag(reportdate);
day=weekday(reportdate);
if first.acct then count=1;
if day=2 and weekday(lg)=6 then count+1;
else if lg=reportdate-1 then count+1;
else count=1;
proc print;run;
/* put data sets back together */
data final;
set other weekends;
by acct reportdate;
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 acct reportdate lg day count
1 11111 08OCT2008 . 4 1
2 11111 09OCT2008 17813 5 2
3 11111 10OCT2008 17814 6 3
4 11111 13OCT2008 17815 2 4
5 11111 16OCT2008 17818 5 1
6 22222 01OCT2008 17821 4 1
7 22222 08OCT2008 17806 4 1
8 22222 10OCT2008 17813 6 1
9 22222 14OCT2008 17815 3 1
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Selecting Data Common Programming Tasks
|
Date Modified: | 2008-11-13 12:57:40 |
Date Created: | 2008-11-05 10:38:57 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9 TS M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
Microsoft Windows 2000 Server | 9 TS M0 | |
Microsoft Windows 2000 Professional | 9 TS M0 | |
Microsoft Windows NT Workstation | 9 TS M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
Microsoft Windows XP Professional | 9 TS M0 | |
64-bit Enabled AIX | 9 TS M0 | |
64-bit Enabled HP-UX | 9 TS M0 | |
64-bit Enabled Solaris | 9 TS M0 | |
HP-UX IPF | 9 TS M0 | |
Linux | 9 TS M0 | |
OpenVMS Alpha | 9 TS M0 | |
Tru64 UNIX | 9 TS M0 | |