I would like to create weekly data from a start date to end date with a week indication variable, see example wanted data.
%let Start= "7feb2020"d;
%let End=''2apr2020"d;
Wanted Data:
Start_week End _week Week_Indicator
7feb2020 13feb2020 1
14feb2020 20feb2020 2
21feb2020 27feb2020 3
28feb2020 5mar2020 4
6mar2020 12mar2020 5
13mar2020 19mar2020 6
20mar2020 26mar2020 7
27mar2020 2apr2020 8
Thank you.
%let Start= "07feb2020"d;
%let End='02apr2020'd;
data want;
start_week=&start;
end_week=.;
do Week_Indicator=1 by 1 while(start_week<&end);
end_week=start_week+6;
output;
start_week=end_week+1;
end;
format start_week end_week date9.;
run;
Do you want macro variables? or data set values?
What do you want to have happen if you provide an end value that is not 7 days from the start of a "week"?
%let Start= "07feb2020"d;
%let End='02apr2020'd;
data want;
start_week=&start;
end_week=.;
do Week_Indicator=1 by 1 while(start_week<&end);
end_week=start_week+6;
output;
start_week=end_week+1;
end;
format start_week end_week date9.;
run;
Here you are a quick solution. I hope it fits. 🙂
%let Start=7feb2020;
%let End=2apr2020;
data weeks (keep=Start_Week End_Week Week_indicator);
format Start_Week End_week date9.;
do i="&Start"d to "&End"d by 7;
Start_Week = i;
End_week = i+6;
Week_indicator + 1;
output;
end;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.