NWKDOM Function

Returns the date for the nth occurrence of a weekday for the specified month and year.

Category: Date and Time

Syntax

NWKDOM(n, weekday, month, year)

Required Arguments

n

specifies the numeric week of the month that contains the specified day.

Range 1–5
Tip N=5 indicates that the specified day occurs in the last week of that month. Sometimes n=4 and n=5 produce the same results.

weekday

specifies the number that corresponds to the day of the week.

Range 1–7
Tip Sunday is considered the first day of the week and has a weekday value of 1.

month

specifies the number that corresponds to the month of the year.

Range 1–12

year

specifies a four-digit calendar year.

Details

The NWKDOM function returns a SAS date value for the nth weekday of the month and year that you specify. Use any valid SAS date format, such as the DATE9. format, to display a calendar date. You can specify n=5 for the last occurrence of a particular weekday in the month.
Sometimes n=5 and n=4 produce the same result. These results occur when there are only four occurrences of the requested weekday in the month. For example, if the month of January begins on a Sunday, there will be five occurrences of Sunday, Monday, and Tuesday, but only four occurrences of Wednesday, Thursday, Friday, and Saturday. In this case, specifying n=5 or n=4 for Wednesday, Thursday, Friday, or Saturday will produce the same result.
If the year is not a leap year, February has 28 days and there are four occurrences of each day of the week. In this case, n=5 and n=4 produce the same results for every day.

Comparisons

In the NWKDOM function, the value for weekday corresponds to the numeric day of the week beginning on Sunday. This value is the same value that is used in the WEEKDAY function, where Sunday =1, and so on. The value for month corresponds to the numeric month of the year beginning in January. This value is the same value that is used in the MONTH function, where January =1, and so on.
You can use the NWKDOM function to calculate events that are not defined by the HOLIDAY function. For example, if a university always schedules graduation on the first Saturday in June, then you can use the following statement to calculate the date: UnivGrad = nwkdom(1, 7, 6, year);

Examples

Example 1: Returning Date Values

The following example uses the NWKDOM function and returns the date for specific occurrences of a weekday for a specified month and year.
data _null_;
      /* Return the date of the third Monday in May 2000. */
   a=nwkdom(3, 2, 5, 2000);
      /* Return the date of the fourth Wednesday in November 2007. */
   b=nwkdom(4, 4, 11, 2007);
      /* Return the date of the fourth Saturday in November 2007. */
   c=nwkdom(4, 7, 11, 2007);
      /* Return the date of the first Sunday in January 2007. */
   d=nwkdom(1, 1, 1, 2007);
      /* Return the date of the second Tuesday in September 2007. */
   e=nwkdom(2, 3, 9, 2007);
      /* Return the date of the fifth Thursday in December 2007. */
   f=nwkdom(5, 5, 12, 2007);
   put a= weekdatx.;
   put b= weekdatx.;
   put c= weekdatx.;
   put d= weekdatx.;
   put e= weekdatx.;
   put f= weekdatx.;
run;
Output from Returning Date Values
a=Monday, 15 May 2000
b=Wednesday, 28 November 2007
c=Saturday, 24 November 2007
d=Sunday, 7 January 2007
e=Tuesday, 11 September 2007
f=Thursday, 27 December 2007

Example 2: Returning the Date of the Last Monday in May

The following example returns the date that corresponds to the last Monday in the month of May in the year 2007.
data _null_;
      /* The last Monday in May. */
   x=nwkdom(5,2,5,2007);
   put x date9.;
run;
The following output is written to the SAS log:
Output from Calculating the Date of the Last Monday in May
28MAY2007