Hello,
I am trying to find a way to sum the totals by day (data have, below) to the total by week (data want, below) without using the if/then statement (since I have over 200 days, this would take too long). I would like to find a more efficient way to do this.
Thanks in advance for your help
data have; input day total; datalines; 1 48 2 29 3 39 4 46 5 35 6 44 7 41 8 46 9 27 10 45 11 38 12 40 13 37 14 80 15 50 16 43 17 54 18 48 19 40 20 68 21 105 ; run;
data want
total | |
Week 1 | 282 |
Week 2 | 313 |
Week 3 | 408 |
@daszlosek Forgive me for 1 if then 🙂
data have;
input day total;
datalines;
1 48
2 29
3 39
4 46
5 35
6 44
7 41
8 46
9 27
10 45
11 38
12 40
13 37
14 80
15 50
16 43
17 54
18 48
19 40
20 68
21 105
;
run;
data want;
do until(last);
Week_total=0;
Week+1;
do _n_=1 by 1 until(_n_=7);
set have end=last;
Week_total+total;
if _n_=7 then output;
end;
end;
keep Week:;
run;
Am i allowed to have one if then statement please? 🙂
@daszlosek Forgive me for 1 if then 🙂
data have;
input day total;
datalines;
1 48
2 29
3 39
4 46
5 35
6 44
7 41
8 46
9 27
10 45
11 38
12 40
13 37
14 80
15 50
16 43
17 54
18 48
19 40
20 68
21 105
;
run;
data want;
do until(last);
Week_total=0;
Week+1;
do _n_=1 by 1 until(_n_=7);
set have end=last;
Week_total+total;
if _n_=7 then output;
end;
end;
keep Week:;
run;
LoL Thank you and have a nice day. Cheers!
data want(where= (wf =0) keep=day c_total wf);
set have;
wf = mod(day,7);
flag = wf ~=1;
retain c_total 0;
c_total = (c_total*flag + total);
run;
Proc SQL approach:
proc sql;
create table want_sql as
select ceil(day/7) as week, sum(total) as w_total
from have
group by calculated week
order by week
;
quit;
@daszlosek my apologies, I should have tested to avoid the IF THEN:
data want;
Week_total=0;
Week+1;
do _n_=1 by 1 until(_n_=7);
set have;
Week_total+total;
end;
keep Week:;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.