Fluorite | Level 6

## Find Tuesdays in a year

I'm trying to find number of Tuesdays in a year using 2 approaches. I want to know why both give different answers!

Approach : 1 --> total tuesday are 53

%let year = 2024;
data count;
tuesday_count = 0;
do date = mdy(1, 1, &year) to mdy(12, 31, &year);
if weekday(date) = 3 then tuesday_count + 1;
end;
output;
keep tuesday_count;
run;

Approach : 2 --> total tuesday can be 52 as 52 rows are generated

data calendar (drop=yr1 yr2);
yr1="01jan2024"D;
yr2="31dec2024"D;
do i=yr1 to yr2;
Day=weekday(i);
output;
end;
format day downame3.;
run;

data new2;
set calendar;
format day downame3.;
where day=4;
run;

I do know approach 1 is giving correct ans. But can anyone plz explain the logic why approach  2 is generating only 52 rows i.e and not 53 rows?

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Find Tuesdays in a year

Hello @POOJAA,

@POOJAA wrote:

I do know approach 1 is giving correct ans. But can anyone plz explain the logic why approach  2 is generating only 52 rows i.e and not 53 rows?

This is because approach 2 counts Wednesdays (weekday 4), not Tuesdays.

@POOJAA wrote:

Approach : 1 --> total tuesday are 53

if weekday(date) = 3 then tuesday_count + 1;

Approach : 2 --> total tuesday can be 52 as 52 rows are generated

Day=weekday(i);
...
where day=4;

There is another mistake in your code with no impact on the count, though:

@POOJAA wrote:

Approach : 2 --> total tuesday can be 52 as 52 rows are generated

Day=weekday(i);
...
format day downame3.;

The DOWNAMEw. format expects SAS date values, not weekday numbers 1 - 7. The latter would be interpreted as the date values '02JAN1960'd, ..., '08JAN1960'd, but those days were Saturday, ..., Friday, not Sunday, ..., Saturday, hence misleading labels. Apply the DOWNAME3. format to the date variable i, not variable day, to see the correct weekday names.

## Re: Find Tuesdays in a year

Hello @POOJAA,

@POOJAA wrote:

I do know approach 1 is giving correct ans. But can anyone plz explain the logic why approach  2 is generating only 52 rows i.e and not 53 rows?

This is because approach 2 counts Wednesdays (weekday 4), not Tuesdays.

@POOJAA wrote:

Approach : 1 --> total tuesday are 53

if weekday(date) = 3 then tuesday_count + 1;

Approach : 2 --> total tuesday can be 52 as 52 rows are generated

Day=weekday(i);
...
where day=4;

There is another mistake in your code with no impact on the count, though:

@POOJAA wrote:

Approach : 2 --> total tuesday can be 52 as 52 rows are generated

Day=weekday(i);
...
format day downame3.;

The DOWNAMEw. format expects SAS date values, not weekday numbers 1 - 7. The latter would be interpreted as the date values '02JAN1960'd, ..., '08JAN1960'd, but those days were Saturday, ..., Friday, not Sunday, ..., Saturday, hence misleading labels. Apply the DOWNAME3. format to the date variable i, not variable day, to see the correct weekday names.

Discussion stats