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

Hi, 

I'm new to SAS, and I know for sure there's an easier way to write this code.

I have a week variable (week) with integer values and I need to create a new variable (weekstart) where I give each week number a date. Dates are 7 day increments from the 1st date (01Nov2018), which is week 0.

I'm pretty sure there's a straightforward answer. Can someone help? Also, week and weekstart should keep going (i.e. there is no end date).

 

data frequencydate;
set frequency;
format weekstart date9.;
if week=0 then weekstart='01Nov2018'd;
else if week= 1 then weekstart='08Nov2018'd;
else if week= 2 then weekstart='15Nov2018'd;
else if week= 3 then weekstart='22Nov2018'd;
else if week= 4 then weekstart='29Nov2018'd;
else if week= 5 then weekstart='06Dec2018'd;
else if week= 6 then weekstart='13Dec2018'd;
else if week= 7 then weekstart='20Dec2018'd;
else if week= 8 then weekstart='27Dec2018'd;
else if week= 9 then weekstart='03Jan2019'd;
else if week= 10 then weekstart='10Jan2019'd;
else if week= 11 then weekstart='17Jan2019'd;
else if week= 12 then weekstart='24Jan2019'd;
else if week= 13 then weekstart='31Jan2019'd;
else if week= 14 then weekstart='07Feb2019'd;
else if week= 15 then weekstart='14Feb2019'd;
else if week= 16 then weekstart='21Feb2019'd;
else if week= 17 then weekstart='28Feb2019'd;
else if week= 18 then weekstart='07Mar2019'd;
else if week= 19 then weekstart='14Mar2019'd;
else if week= 20 then weekstart='21Mar2019'd;
else if week= 21 then weekstart='28Mar2019'd;
run;

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

weekstart='01Nov2018'd + 7 * week;

View solution in original post

1 REPLY 1
WarrenKuhfeld
Ammonite | Level 13

weekstart='01Nov2018'd + 7 * week;