Lavaparty1999
Lavaparty1999

Reputation: 21

count number of days in the first week sas

I'm new to sas and would like learn to calculate:

How many days in the first week of the year?

For example:

Year = 2016, Week = 1

num_days: 2 days

Year = 2020, week = 1

num_days: 4 days

Thanks in advance

Upvotes: 2

Views: 164

Answers (2)

Stu Sztukowski
Stu Sztukowski

Reputation: 12909

Use intnx() to get the last day in the first week, then count the number of days between the first day of the year and the last day in the first week if the year.

data want;
    date     = '01JAN2016'd;
    week_end = intnx('week', date, 0, 'E');
    num_days = week_end-date+1;

    format date week_end date9.;
run;

Output:

date        week_end    num_days
01JAN2016   02JAN2016   2
01JAN2020   04JAN2020   4

Upvotes: 2

Shenglin Chen
Shenglin Chen

Reputation: 4554

Weekday could convert date to week day, the count begin from Sunday, so Sunday is 1, Saturday is 7. First, convert date of new year day to week day, then cumulate days to Saturday.

data _null_;
x=weekday('01jan2020'd);
do i=x to 7;
  weekday+1;
end;
put x weekday=;
run;  

Upvotes: 2

Related Questions