Compute means or sums over variables

Last changed: 10 February 2021

If you want to compute mean values or sums over different variables, i.e. row means and sums,  you can use the functions sum and mean. Say you have a data set like this:

data weeksales;
input  week   monday   tuesday   wednesday   thursday   friday;
1 4 6 8 6 8
2 3 2 1 3 1
3 7 6 5 3 6
4 6 4 3 2 6
5 3 4 1 6 7

 and you want to compute the sum or mean for each week:

data totalsale;
set weeksales2;
weeklysum=sum(monday, tuesday, wednesday, thursday, friday);
weeklymean=mean(monday, tuesday, wednesday, thursday, friday);


Or you can use the internal ordering of the variables:

data totalsale;
set weeksales;
weeklysum=sum(of monday -- friday);
weeklymean=mean(of monday -- friday);


Some functions that can be used over variables are

MAX(argument,...)        returns the largest value 
MIN(argument,...)        returns the smallest value 
MEAN(argument,...)      returns the arithmetic mean (average) 
N(argument,....)           returns the number of nonmissing arguments 
NMISS(argument,...)  returns the number of missing values 
STD(argument,...)        returns the standard deviation 
STDERR(argument,...) returns the standard error of the mean 
SUM(argument,…)            returns the sum
VAR(argument,...)   returns the variance