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

Hi All,

I need help on macro for getting concurrent weeks. Below an example for 5 customers in campaigns where the start and end dates are different. I need a macro to state which week they fall into. As you can customer A started to campaign on 01Jan -19 and finished on 05 March so I need to count them in all weeks until they reach the end date 

 

CustomerStart dateEnd Datewk1_2019wk2_2019wk3_2019……Wk9_2019wk10_2019wk11_2019wk12_2019wk13_2019wk14_2019wk15_2019
A01/01/201905/03/201911111000000
B05/01/201907/03/2019111111     
C01/03/201910/03/2019111111     
D05/03/201925/03/2019    111    
E20/03/201910/04/2019      111  
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Compare my code to yours, especially the arguments to the year() and week() function.

Note that I had to make up the variable names, as you did not provide example data in usable form (data step with datalines). And the column names in your original post are clearly invalid, as they contain blanks.

It's up to you to insert the correct variable names as they are in your dataset.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

No need for a macro. All this can be done in a data step:

data want;
set have;
do date = start_date to end_date by 7;
  year = year(date)
  week = week(date);
  output;
end;
keep customer year week;
run;

With this, you can easily get counts of customers per year and week.

meckarthik
Quartz | Level 8

Thank you .. but i'm getting below error, i'm in learning stage!!

 

25 GOPTIONS ACCESSIBLE;
26 data test;
27 set Campaign_extract;
28 do date = start_date to end_date by 7;
29 year = year(StartDate);
30 week = week(StartDate);
31 output;
32 end;
33 keep LMCampNo year week;
34 run;

NOTE: Variable start_date is uninitialized.
NOTE: Variable end_date is uninitialized.
ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero,
or invalid.
LMCampNo=12345 StartDate=26OCT2017 EndDate=30SEP2018 RevenueBudget=100  ClockNumber=ABD/123/567 IDSAdSmartableHouseHolds=0
Status=A LMContractedImpressions=100LMDeliveredImpressions=10ImpressionCap=0 DailyImpressionCap=17
Years=2017 date=. start_date=.
end_date=. week=. _ERROR_=1 _N_=1

meckarthik
Quartz | Level 8
Thank you .. but i'm getting below error, i'm in learning stage!!



25 GOPTIONS ACCESSIBLE;
26 data test;
27 set Campaign_extract;
28 do date = start_date to end_date by 7;
29 year = year(StartDate);
30 week = week(StartDate);
31 output;
32 end;
33 keep LMCampNo year week;
34 run;

NOTE: Variable start_date is uninitialized.
NOTE: Variable end_date is uninitialized.
ERROR: Invalid DO loop control information, either the INITIAL or TO
expression is missing or the BY expression is missing, zero,
or invalid.
LMCampNo=12345 StartDate=26OCT2017 EndDate=30SEP2018 RevenueBudget=100
ClockNumber=ABD/123/567 IDSAdSmartableHouseHolds=0
Status=A
LMContractedImpressions=100LMDeliveredImpressions=10ImpressionCap=0
DailyImpressionCap=17
Years=2017 date=. start_date=.
end_date=. week=. _ERROR_=1 _N_=1
Kurt_Bremser
Super User

Compare my code to yours, especially the arguments to the year() and week() function.

Note that I had to make up the variable names, as you did not provide example data in usable form (data step with datalines). And the column names in your original post are clearly invalid, as they contain blanks.

It's up to you to insert the correct variable names as they are in your dataset.

meckarthik
Quartz | Level 8

Many thanks !

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 881 views
  • 1 like
  • 2 in conversation