Hi
I have a dataset with weekly observations for all variables. The format of "Date_week" is "mmddyy10." For every variable, I have one observation per week, but it is not necessarily the last working day of the week.
I want to find out "week number" for every month. I have used following code, but it does not give me the exact week number for the dates that are not the last working day (Friday) of a week.
Please guide me in this regard. Thanks.
data tmp1;
set tmp;
Week = ceil( day(Date_week) / 7); run;
Or you could count the number of Sunday.
data _null_;
n=0;
date='26dec2019'd;
do i=intnx('month',date,0) to date;
if weekday(i)=1 then n+1;
end;
put n=;
run;
If you have weekly data then it may be more useful and definitely easier to explore the SAS WEEK function which will give you the week number in a year. There are different definitions for this so you can choose the one which is most appropriate to your requirements:
Week number in a month is likely to cause boundary problems because of the varying number of days in each month. The WEEK function takes care of those types of problems, including dealing with leap years.
data _null_;
date='26dec2019'd;
n=week(date,'u')-week(intnx('month',date,-1,'e'),'u');
/*
n=week(date,'v')-week(intnx('month',date,-1,'e'),'v');
*/
put n=;
run;
Or you could count the number of Sunday.
data _null_;
n=0;
date='26dec2019'd;
do i=intnx('month',date,0) to date;
if weekday(i)=1 then n+1;
end;
put n=;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.