BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma8
Quartz | Level 8

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

%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;

View solution in original post

3 REPLIES 3
ballardw
Super User

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"?

novinosrin
Tourmaline | Level 20

%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;
KlausBücher
Fluorite | Level 6

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1081 views
  • 2 likes
  • 4 in conversation