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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.