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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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