DATA Step, Macro, Functions and more

Summing multiple variables by group

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Summing multiple variables by group

Hello, 

 

I'm working to create a function that will sum multiple variables by groups. I know how to sum one variable by group using a retain statement, but I'm having trouble looping through variables. 

 

The data is in this format: 

Group value000 value001 value002 value003 value004 value005
1 1 2 3 4 5 6
1 7 8 9 10 11 12
1 13 14 15 16 17 18
1 19 20 21 22 23 24
1 25 26 27 28 29 30
1 31 32 33 34 35 36
1 37 38 39 40 41 42
2 43 44 45 46 47 48
2 49 50 51 52 53 54
2 55 56 57 58 59 60
2 61 62 63 64 65 66
2 67 68 69 70 71 72

 

I'd like the data to be in this format, that is 1 row for each group, and a sum of all values in said group. The actual dataset will have thousands of groups, and 41 variables per group (all in the format valuexxx). The number of observations per group will vary between 4-50.

 

Group value000 value001 value002 value003 value004 value005
1 133 140 147 154 161 168
2 275 280 285 290 295 300

Accepted Solutions
Solution
‎08-23-2017 09:12 AM
Super User
Posts: 23,309

Re: Summing multiple variables by group

Posted in reply to jbscholten

Try using a summary procedure, such as PROC MEANS instead. This is a trivial calculation to summarize data. 

 

proc means data=have nway;
class group;
var value000-value005;
output out=want  sum = ;
run;

A data step would be inefficient and tedious to program. 

 


jbscholten wrote:

Hello, 

 

I'm working to create a function that will sum multiple variables by groups. I know how to sum one variable by group using a retain statement, but I'm having trouble looping through variables. 

 

The data is in this format: 

Group value000 value001 value002 value003 value004 value005
1 1 2 3 4 5 6
1 7 8 9 10 11 12
1 13 14 15 16 17 18
1 19 20 21 22 23 24
1 25 26 27 28 29 30
1 31 32 33 34 35 36
1 37 38 39 40 41 42
2 43 44 45 46 47 48
2 49 50 51 52 53 54
2 55 56 57 58 59 60
2 61 62 63 64 65 66
2 67 68 69 70 71 72

 

I'd like the data to be in this format, that is 1 row for each group, and a sum of all values in said group. The actual dataset will have thousands of groups, and 41 variables per group (all in the format valuexxx). The number of observations per group will vary between 4-50.

 

Group value000 value001 value002 value003 value004 value005
1 133 140 147 154 161 168
2 275 280 285 290 295 300

 

 

View solution in original post


All Replies
Solution
‎08-23-2017 09:12 AM
Super User
Posts: 23,309

Re: Summing multiple variables by group

Posted in reply to jbscholten

Try using a summary procedure, such as PROC MEANS instead. This is a trivial calculation to summarize data. 

 

proc means data=have nway;
class group;
var value000-value005;
output out=want  sum = ;
run;

A data step would be inefficient and tedious to program. 

 


jbscholten wrote:

Hello, 

 

I'm working to create a function that will sum multiple variables by groups. I know how to sum one variable by group using a retain statement, but I'm having trouble looping through variables. 

 

The data is in this format: 

Group value000 value001 value002 value003 value004 value005
1 1 2 3 4 5 6
1 7 8 9 10 11 12
1 13 14 15 16 17 18
1 19 20 21 22 23 24
1 25 26 27 28 29 30
1 31 32 33 34 35 36
1 37 38 39 40 41 42
2 43 44 45 46 47 48
2 49 50 51 52 53 54
2 55 56 57 58 59 60
2 61 62 63 64 65 66
2 67 68 69 70 71 72

 

I'd like the data to be in this format, that is 1 row for each group, and a sum of all values in said group. The actual dataset will have thousands of groups, and 41 variables per group (all in the format valuexxx). The number of observations per group will vary between 4-50.

 

Group value000 value001 value002 value003 value004 value005
1 133 140 147 154 161 168
2 275 280 285 290 295 300

 

 

Esteemed Advisor
Posts: 5,482

Re: Summing multiple variables by group

Posted in reply to jbscholten

Using a variable list, you can write a data step:

 

data want;
do until (last.group);
    set have; by group;
    array v{*} value:;
    array s{50};
    do i = 1 to dim(v);
        s{i} = sum(s{i}, v{i});
        end;
    end;
do i = 1 to dim(v);
    v{i} = s{i};
    end;
output;
keep Group value:;
run;
PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1318 views
  • 3 likes
  • 3 in conversation