BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
POOJAA
Fluorite | Level 6

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
FreelanceReinh
Jade | Level 19

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.

 

View solution in original post

1 REPLY 1
FreelanceReinh
Jade | Level 19

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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 176 views
  • 2 likes
  • 2 in conversation