How can I calculate the sum across columns in SAS with an example? 2

How can I calculate the sum across columns in SAS with an example?

Calculating the sum across columns in SAS is a simple process that allows you to add up the values in specific columns and obtain a total sum. To do this, you can use the SUM function in SAS, which takes in the column names as arguments and returns the sum of the values in those columns. For example, if you have a data set with columns A, B, and C, and you want to calculate the sum of the values in column B and C, you can use the following code:

SUM(B, C);

This will return the sum of all the values in columns B and C. This method can be useful for data analysis and creating summary statistics in SAS. It is also possible to calculate the sum across multiple rows and columns by specifying the range of cells or using the SUM function in combination with other SAS functions.

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 printdata=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 printdata=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:

Cite this article

stats writer (2024). How can I calculate the sum across columns in SAS with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-across-columns-in-sas-with-an-example/

stats writer. "How can I calculate the sum across columns in SAS with an example?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-across-columns-in-sas-with-an-example/.

stats writer. "How can I calculate the sum across columns in SAS with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-across-columns-in-sas-with-an-example/.

stats writer (2024) 'How can I calculate the sum across columns in SAS with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-sum-across-columns-in-sas-with-an-example/.

[1] stats writer, "How can I calculate the sum across columns in SAS with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate the sum across columns in SAS with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top