Hello, Happy New Year to All!
I wanted to ask assistance in adjusting the below code, I'm trying to get Weekdates for 2024 that ends in Tuesdays.
below is the code I am using and I am not getting the first week of this year which is 01/02/2024, can someone look into it? thanks a lot.
*Report uses Tuesday's EOD inventory;
data WeekDates;
*Preparing a list of holidays that land on a Tuesday
and a list of Mondays to use in lieu of;
if _N_ = 0 then do;
%let HOLIDAYS =;
%let PROXY_DAYS =;
array STAT_HOLIDAY[4] $9. _temporary_ ('newyear', 'canada', 'christmas', 'boxing');
do YEAR=&BEGIN_YEAR to &END_YEAR;
do i=1 to 4;
if weekday(holiday(STAT_HOLIDAY[i],YEAR)) = 3 then do;
call symputx('HOLIDAYS',catx(', ',resolve('&HOLIDAYS'),"'"||put(holiday(STAT_HOLIDAY[i],YEAR),mmddyy10.)||"'"));
call symputx('PROXY_DAYS',catx(', ',resolve('&PROXY_DAYS'),"'"||put(intnx('day',holiday(STAT_HOLIDAY[i],YEAR),-1),mmddyy10.)||"'"));
end;
end;
end;
end;
*Check if the first day of the year is in the same week as any dates in the previous year.
If so, SAS will treat this week as week zero, so bump the week up by 1 to accommodate;
if week(intnx('week',intnx('year',mdy(1,1,&END_YEAR),0),0)+2) = 1 then do i=0 to 52; /*** changed from =0 to =1 to reflect first week of 2023 ***/
WEEK=i+1;
do j=1 to 4;
*Check if the PORTFOLIO_WEEK will be a holiday, which will cause dips in the data
due to stores being closed;
if holiday(STAT_HOLIDAY[j],&END_YEAR) = intnx('week',mdy(1,1,&END_YEAR),i)+2 then do;
*If Tuesday is a holiday, use the previous day, i.e. Monday's EOD inventory instead;
PORTFOLIO_WEEK = intnx('week',mdy(1,1,&END_YEAR),i)+1;
*Leave the DO loop as soon as there is a TRUE condition, otherwise it could overwrite
on the subsequent iteration;
leave;
end;
else PORTFOLIO_WEEK = intnx('week',mdy(1,1,&END_YEAR),i)+2;
end;
output;
end;
else do i=1 to 52;
WEEK=i;
do j=1 to 4;
if holiday(STAT_HOLIDAY[j],&END_YEAR) = intnx('week',mdy(1,1,&END_YEAR),i)+2 then do;
PORTFOLIO_WEEK = intnx('week',mdy(1,1,&END_YEAR),i)+1;
leave;
end;
else PORTFOLIO_WEEK = intnx('week',mdy(1,1,&END_YEAR),i)+2;
end;
output;
end;
format PORTFOLIO_WEEK mmddyy10.;
drop i j year;
run;
@Eugenio211 wrote:
I'm trying to get Weekdates for 2024 that ends in Tuesdays.
The language used here isn't clear to me. "Weekdates ... that ends in Tuesday" doesn't have an obvious meaning to me. Could you explain this further? I assumed (perhaps incorrectly) that you want all Tuesdays in a year, or perhaps that is just one step in the process that you really want.
Here is a way to generate all Tuesdays.
data tuesdays;
first_Tuesday_of_the_year=intnx('week.3','01JAN2024'd,1);
tuesday=first_Tuesday_of_the_year;
do while(tuesday<='31DEC2024'd);
output;
tuesday=tuesday+7;
end;
format tuesday first_Tuesday_of_the_year date9.;
run;
Has this process ever worked? That is did it work for 2023 but it is now failing for 2024?
It is very hard to read code that is not indented consistently.
You seem to be confused about the order of operations between macro code and SAS code. Why do you have %LET statements in the MIDDLE of a data step?
Your first block of code will never run since you have it enclosed in a IF/THEN that can never be true.
if _N_ = 0 then do;
If you want to find all of the Tuesdays and test if they fall on one of those four holidays why not just do that? Note that none of those holidays fall on Tuesday in 2024.
In fact the only time any of them fell on a Tuesday in the last 5 years was Boxing Day of 2023.
data test;
length holiday $20 date 8;
do year=2020 to 2024;
do holiday='newyear','canada','christmas','boxing';
date=holiday(holiday,year);
if weekday(date)=3 then output;
end;
end;
format date yymmdd10.;
run;
proc print;
run;
So do you want to test if the Start of the week ending on Tuesday is a holiday?
data tuesdays;
length year week start tuesday 8 holiday $20 found 8;
year=2024;
week=0;
do tuesday = NWKDOM(1,3,1,year) to mdy(12,31,year) by 7;
week+1;
found=0;
do holiday='newyear','canada','christmas','boxing';
if tuesday-6 = holiday(holiday,year) then do; found=1; leave; end;
end;
start = tuesday - 6 ;
if found then start=start-1;
else holiday=' ';
output;
end;
format tuesday start yymmdd10.;
run;
Result:
OBS year week start tuesday holiday found 1 2024 1 2023-12-27 2024-01-02 0 2 2024 2 2024-01-03 2024-01-09 0 ... 52 2024 52 2024-12-18 2024-12-24 0 53 2024 53 2024-12-24 2024-12-31 christmas 1
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.