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

 

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
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

 

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Am i allowed to have one if then statement please? 🙂

novinosrin
Tourmaline | Level 20

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

 

 

daszlosek
Quartz | Level 8
Ha! Ofcourse! I just meant I didn't want to do a whole bunch of if 14<= day >1 then week = "week 1" sort of thing!!
novinosrin
Tourmaline | Level 20

LoL Thank you and have a nice day. Cheers!

sk423
Obsidian | Level 7
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;
Haikuo
Onyx | Level 15

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;
novinosrin
Tourmaline | Level 20

@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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 1312 views
  • 9 likes
  • 4 in conversation