NWKDOM Function

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

Categories:Date and Time
CAS

Syntax

NWKDOM(n, weekday, month, year)

Required Arguments

n

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

Range1–5
TipN=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.

Range1–7
TipSunday 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.

Range1–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 2012. */
   a=nwkdom(3, 2, 5, 2012);
      /* Return the date of the fourth Wednesday in November 2012. */
   b=nwkdom(4, 4, 11, 2012);
      /* Return the date of the fourth Saturday in November 2012. */
   c=nwkdom(4, 7, 11, 2012);
      /* Return the date of the first Sunday in January 2013. */
   d=nwkdom(1, 1, 1, 2013);
      /* Return the date of the second Tuesday in September 2012. */
   e=nwkdom(2, 3, 9, 2012);
      /* Return the date of the fifth Thursday in December 2012. */
   f=nwkdom(5, 5, 12, 2012);
   put a= weekdatx.;
   put b= weekdatx.;
   put c= weekdatx.;
   put d= weekdatx.;
   put e= weekdatx.;
   put f= weekdatx.;
run;
SAS writes the following output to the log:
a=Monday, 21 May 2012
b=Wednesday, 28 November 2012
c=Saturday, 24 November 2012
d=Sunday, 6 January 2013
e=Tuesday, 11 September 2012
f=Thursday, 27 December 2012

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 2012 .
data _null_;
      /* The last Monday in May. */
   x=nwkdom(5, 2, 5, 2012);
   put x date9.;
run;
SAS writes the following output to the log:
28MAY2012
Last updated: March 16, 2017