How to Sum Across Columns in SAS (With Example)

You can use the following syntax to sum across columns in a dataset in SAS:

data new_data;
    set my_data;
    sum_stats = sum(of points, assists, rebounds);
run;

This particular example creates a new dataset that contains a new column called sum_stats that contains the sum of the values in the columns called points, assists, and rebounds.

The following example shows how to use this syntax in practice.

Example: How to Sum Across Columns in SAS

Suppose we have the following dataset in SAS called my_data that contains information about various basketball players:

/*create dataset*/
data my_data;
input team $ points assists rebounds;
datalines;
A 10 2 4
A 17 5 9
A 17 6 8
A 18 3 8
A 15 0 6
B 10 2 3
B 14 5 3
B 13 4 3
B 29 0 6
B 25 2 5
C 12 1 4
C 30 1 9
C 34 3 9
C 12 4 5
C 11 7 5
;
run;

/*view dataset*/
proc print data=my_data;

Now suppose that we would like to create a new column called sum_stats that contains the sum of the values in the columns called points, assists, and rebounds.

We can use the following code to do so:

/*create new dataset that contains sum of specific columns*/
data new_data;
    set my_data;
    sum_stats = sum(of points, assists, rebounds);
run;

/*view new dataset*/
proc print data=new_data;

SAS sum across columns

Notice that the column called sum_stats contains the sum of each value in the points, assists, and rebounds columns.

For example:

The sum of points, assists, and rebounds in the first row is 10 + 2 + 4 = 16.

The sum of points, assists, and rebounds in the second row is 17 + 5 + 9 = 31.

And so on.

The following tutorials explain how to perform other common tasks in SAS:

x