## Multiple variables in sum-function

Solved
Occasional Contributor
Posts: 12

# Multiple variables in sum-function

Hi everyone,

I have a dataset with 90 variables, when I want to create a sum-function with the Query Builder I have to dubble-click on each variable.

Is there a way to select all the variables faster? Ideal would be something like: sum(var2 [until] var90).

Thanks,

David

Accepted Solutions
Solution
‎02-19-2013 02:46 PM
Super User
Posts: 8,215

## Re: Multiple variables in sum-function

I would use the sum function in a datastep for that.  e.g.,

data have;

input var1 \$ var2-var6;

group1=sum(var2,var3);

group2=sum(of var4-var6);

cards;

A    4      8     6     9     1

B    9      5     3     0     2

C    2      3     2     3     5

D    1      2     5     9     1

E    9      7     3     4     3

;

All Replies
Super User
Posts: 8,215

## Re: Multiple variables in sum-function

Do you need do to it with the Query Builder?  It would be a trivial request using proc summary.

Occasional Contributor
Posts: 12

## Re: Multiple variables in sum-function

Well for this step the proc summary is perfect.

But in a later step I need the group them in 4 big groups... (and I don't know if that's possible with the proc summary)

Super User
Posts: 8,215

## Re: Multiple variables in sum-function

You would have to explain what you mean by 4 big groups.  If it is groups of records, you can use a by variable.  If it is groups of variables, you can first use proc transpose (to make the table wide) and then simply use proc summary on the wide tables.

Occasional Contributor
Posts: 12

## Re: Multiple variables in sum-function

Yes, 4 groups of variables.

Maybe it's more clear with a little example:

var1 var2 var3 var4 var5 var6

A    4      8     6     9     1

B    9      5     3     0     2

C    2      3     2     3     5

D    1      2     5     9     1

E    9      7     3     4     3

And the result should be a new dataset:

var1 group1 group2

A     12       16

B     14       5

C      5        10

...

group1: sum(var1, var2)

group2: sum(var3, var4, var5)

Solution
‎02-19-2013 02:46 PM
Super User
Posts: 8,215

## Re: Multiple variables in sum-function

I would use the sum function in a datastep for that.  e.g.,

data have;

input var1 \$ var2-var6;

group1=sum(var2,var3);

group2=sum(of var4-var6);

cards;

A    4      8     6     9     1

B    9      5     3     0     2

C    2      3     2     3     5

D    1      2     5     9     1

E    9      7     3     4     3

;

Occasional Contributor
Posts: 12

## Re: Multiple variables in sum-function

This was exactly what I was looking for! Thank you!

🔒 This topic is solved and locked.