Nathan123
Nathan123

Reputation: 763

how to sum observations in SAS by groups?

I have an example of a dataset in the following manner

data have;
input match percent;
cards;
0   34
0   54
0   33
0   23
1   60  
1   70
1   70
1   70
;

Essentially I want to sum the observations that are associated with 0 and then divide them by the number of 0s to find the average. e.g 34+54+33+23/4 then do the same for 1's

I looked at PROC TABULATE. However, I don't understand how to carry out this procedure.

Upvotes: 0

Views: 1002

Answers (3)

momo1644
momo1644

Reputation: 1804

You can use proc means and you will the mean plus a bunch of other stats: more examples here for proc means.

proc means data=have  noprint;
by match;
output out=want ;

Output:

output mean

Upvotes: 1

J_Lard
J_Lard

Reputation: 1103

This can be done very easily using proc summary or proc means.

proc summary data=have nway missing;
class match;
var percent;
output out=want mean=;
run;

You can also output a variety of other statistics using these procedures.

Upvotes: 0

DomPazz
DomPazz

Reputation: 12465

Many ways to do this in SAS. I would use PROC SQL

proc sql noprint;
create table want as
select match, 
       mean(percent) as percent
   from have
   group by match;
quit;

Upvotes: 1

Related Questions