Data about each month (SAS)

Do you remember this old old nursery rhyme?

30 days hath September,
April, June and November,
All the rest have 31,
Excepting February alone
(And that has 28 days clear,
With 29 in each leap year).

Ne neither, and I don’t need to because I use SAS code to generate information about each month, including the number of days in each month, using SAS’s powerful intnx and intck functions to do the hard work. It’s also a good example of using a DATA step to generate a data set without an input data set (i.e., no SET or MERGE statements).

data months;
	format first_day last_day yymmdd10.;
	do year = 2013 to 2020;
		do month = 1 to 12;
			first_day = mdy(month, 1, year);
			last_day = intnx('month', first_day, 0, 'e');
			days  = intck('day', first_day, last_day) + 1;
			weekdays  = intck('weekday', first_day, last_day) + 1;
			weeks  = intck('week', first_day, last_day);
			hours  = intck('dthour', dhms(first_day, 0, 0,0), dhms(last_day, 23, 59, 59)) + 1;
			work_hours = weekdays * 8;
			output;
		end; /* end month */
	end; /* end year */
	label
		first_day = 'First day'
		last_day = 'Last day'
		days = 'Days'
		weekdays = 'Week days'
		weeks = 'Weeks'
		hours = 'Hours'
		work_hours = 'Work hours'
		;
	drop month year;
run;

Here are the results:

First day Last day Days Week days Weeks Hours Work hours
1/1/2013 1/31/2013 31 23 4 744 184
2/1/2013 2/28/2013 28 20 4 672 160
3/1/2013 3/31/2013 31 21 5 744 168
4/1/2013 4/30/2013 30 22 4 720 176
5/1/2013 5/31/2013 31 23 4 744 184
6/1/2013 6/30/2013 30 21 5 720 168
7/1/2013 7/31/2013 31 23 4 744 184
8/1/2013 8/31/2013 31 22 4 744 176
9/1/2013 9/30/2013 30 22 4 720 176
10/1/2013 10/31/2013 31 23 4 744 184
11/1/2013 11/30/2013 30 21 4 720 168
12/1/2013 12/31/2013 31 23 4 744 184
1/1/2014 1/31/2014 31 23 4 744 184
2/1/2014 2/28/2014 28 21 4 672 168
3/1/2014 3/31/2014 31 22 5 744 176
4/1/2014 4/30/2014 30 22 4 720 176
5/1/2014 5/31/2014 31 22 4 744 176
6/1/2014 6/30/2014 30 22 4 720 176
7/1/2014 7/31/2014 31 23 4 744 184
8/1/2014 8/31/2014 31 21 5 744 168
9/1/2014 9/30/2014 30 22 4 720 176
10/1/2014 10/31/2014 31 23 4 744 184
11/1/2014 11/30/2014 30 21 5 720 168
12/1/2014 12/31/2014 31 23 4 744 184
1/1/2015 1/31/2015 31 22 4 744 176
2/1/2015 2/28/2015 28 21 3 672 168
3/1/2015 3/31/2015 31 23 4 744 184
4/1/2015 4/30/2015 30 22 4 720 176
5/1/2015 5/31/2015 31 21 5 744 168
6/1/2015 6/30/2015 30 22 4 720 176
7/1/2015 7/31/2015 31 23 4 744 184
8/1/2015 8/31/2015 31 22 5 744 176
9/1/2015 9/30/2015 30 22 4 720 176
10/1/2015 10/31/2015 31 22 4 744 176
11/1/2015 11/30/2015 30 22 4 720 176
12/1/2015 12/31/2015 31 23 4 744 184
1/1/2016 1/31/2016 31 21 5 744 168
2/1/2016 2/29/2016 29 21 4 696 168
3/1/2016 3/31/2016 31 23 4 744 184
4/1/2016 4/30/2016 30 21 4 720 168
5/1/2016 5/31/2016 31 23 4 744 184
6/1/2016 6/30/2016 30 22 4 720 176
7/1/2016 7/31/2016 31 21 5 744 168
8/1/2016 8/31/2016 31 23 4 744 184
9/1/2016 9/30/2016 30 22 4 720 176
10/1/2016 10/31/2016 31 22 5 744 176
11/1/2016 11/30/2016 30 22 4 720 176
12/1/2016 12/31/2016 31 22 4 744 176
1/1/2017 1/31/2017 31 23 4 744 184
2/1/2017 2/28/2017 28 20 4 672 160
3/1/2017 3/31/2017 31 23 4 744 184
4/1/2017 4/30/2017 30 21 5 720 168
5/1/2017 5/31/2017 31 23 4 744 184
6/1/2017 6/30/2017 30 22 4 720 176
7/1/2017 7/31/2017 31 22 5 744 176
8/1/2017 8/31/2017 31 23 4 744 184
9/1/2017 9/30/2017 30 21 4 720 168
10/1/2017 10/31/2017 31 23 4 744 184
11/1/2017 11/30/2017 30 22 4 720 176
12/1/2017 12/31/2017 31 21 5 744 168
1/1/2018 1/31/2018 31 23 4 744 184
2/1/2018 2/28/2018 28 20 4 672 160
3/1/2018 3/31/2018 31 22 4 744 176
4/1/2018 4/30/2018 30 22 4 720 176
5/1/2018 5/31/2018 31 23 4 744 184
6/1/2018 6/30/2018 30 21 4 720 168
7/1/2018 7/31/2018 31 23 4 744 184
8/1/2018 8/31/2018 31 23 4 744 184
9/1/2018 9/30/2018 30 21 5 720 168
10/1/2018 10/31/2018 31 23 4 744 184
11/1/2018 11/30/2018 30 22 4 720 176
12/1/2018 12/31/2018 31 22 5 744 176
1/1/2019 1/31/2019 31 23 4 744 184
2/1/2019 2/28/2019 28 20 4 672 160
3/1/2019 3/31/2019 31 21 5 744 168
4/1/2019 4/30/2019 30 22 4 720 176
5/1/2019 5/31/2019 31 23 4 744 184
6/1/2019 6/30/2019 30 21 5 720 168
7/1/2019 7/31/2019 31 23 4 744 184
8/1/2019 8/31/2019 31 22 4 744 176
9/1/2019 9/30/2019 30 22 4 720 176
10/1/2019 10/31/2019 31 23 4 744 184
11/1/2019 11/30/2019 30 21 4 720 168
12/1/2019 12/31/2019 31 23 4 744 184
1/1/2020 1/31/2020 31 23 4 744 184
2/1/2020 2/29/2020 29 21 4 696 168
3/1/2020 3/31/2020 31 23 4 744 184
4/1/2020 4/30/2020 30 22 4 720 176
5/1/2020 5/31/2020 31 21 5 744 168
6/1/2020 6/30/2020 30 22 4 720 176
7/1/2020 7/31/2020 31 23 4 744 184
8/1/2020 8/31/2020 31 22 5 744 176
9/1/2020 9/30/2020 30 22 4 720 176
10/1/2020 10/31/2020 31 22 4 744 176
11/1/2020 11/30/2020 30 22 4 720 176
12/1/2020 12/31/2020 31 23 4 744 184

Tested with SAS 9.3 on Windows 7.

Leave a comment