To sum by group in SAS, you can use PROC MEANS and specify by group in the CLASS statement. You can sum multiple variables and group by multiple groups with PROC MEANS.

```
data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;
proc means data=example sum;
class group;
variable value;
run;
```

You can also output this dataset with the OUTPUT statement.

```
data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;
proc means data=example sum;
class group;
variable value;
output out=sum_by_group sum(value)=;
run;
/* Output */
group _TYPE_ _FREQ_ value
0 8 36
A 1 3 6
B 1 3 15
C 1 2 15
```

Use the NWAY option if you only want to create a dataset with the sums by group (instead of the entire dataset, for example).

```
data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;
proc means data=example sum nway;
class group;
variable value;
output out=sum_by_group sum(value)=;
run;
/* Output */
group _TYPE_ _FREQ_ value
A 1 3 6
B 1 3 15
C 1 2 15
```

When working with data, the ability to summarize and aggregate your data in different ways is very valuable.

One such case is if you want to sum your data by groups.

In SAS, PROC MEANS is a procedure which allows you to create summaries of your data and allows you to calculate things like the sum, mean, min, max, etc. of a variable.

You can use PROC MEANS to sum variables by group using the CLASS statement.

Below is a simple example of how you can use PROC MEANS to sum by group in SAS.

```
data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;
proc means data=example sum;
class group;
variable value;
run;
```

## How to Sum By Multiple Groups in SAS with PROC MEANS

If you want to sum by multiple groups in SAS, you can add variables to the PROC MEANS CLASS statement.

Below shows you a simple example of summing by multiple groups in SAS with PROC MEANS.

```
data example;
input group1 $ group2 $ value;
datalines;
A D 1
A D 2
A E 3
B E 4
B E 5
B F 6
C F 7
C F 8
;
run;
proc means data=example sum;
class group1 group2;
variable value;
run;
```

## How to Sum Multiple Variables by Group in SAS with PROC MEANS

If you want to sum multiple values by group in SAS, you can add variables to the PROC MEANS VARIABLE statement.

Below shows you a simple example of summing multiple variables by group in SAS with PROC MEANS.

```
data example;
input group $ value1 value2;
datalines;
A 1 2
A 2 3
A 3 4
B 4 5
B 5 6
B 6 7
C 7 8
C 8 9
;
run;
proc means data=example sum;
class group;
variable value1 value2;
run;
```

Hopefully this article has been useful for you to learn how to sum by groups using PROC MEANS in SAS.

## Leave a Reply