user3311403
user3311403

Reputation: 867

How to find the mean value of columns in SAS

I have a SAS data set, let's say:

No d1 d2 d3 ... dn
1  2  3  4 ... n1
2  3  4  5 ... n2
3  4  5  6 ... n3

I now need to find the averages of all columns in SAS. Is there a way to do it?

The number of columns is not specific. If I need the averages of d1-dn columns, then the output I expect is:

3 4 5 ..  (n1+n2+n3)/3

Is there a way to do this Either in the data step or using proc sql or proc iml?

Upvotes: 5

Views: 30883

Answers (4)

Longfish
Longfish

Reputation: 7602

Assuming all your required variables begin with d, then you can use the colon wildcard operator to select them all. I've used PROC SUMMARY here, this is identical to PROC MEANS with the NOPRINT option. Obviously this is a very minor alteration of the answer from @pteranodon

proc summary data=have nway;
var d: ;
output out=want (drop=_:) mean=;
run;

Upvotes: 4

Evan Volgas
Evan Volgas

Reputation: 2911

You can use PROC MEANS, eg,

PROC MEANS DATA = indata; 
VAR d1-dn; 
OUTPUT=outdata MEAN=m1-mn; 
RUN;

Upvotes: 1

pteranodon
pteranodon

Reputation: 2059

proc means is the way to calculate column-wise statistics.

data have;
  input No d1 d2 d3 d4;
datalines;
1  2  8 60 80
2  3  12 50 70
3  4  10 40 60
;
run;

proc means data=have noprint nway;
  var d1-d4;
  output 
    out=want(drop=_TYPE_ _FREQ_) 
    mean=;
run;

The drop= clause will exclude the automatic variables for type and frequency if you don't need them.

Upvotes: 6

user1509107
user1509107

Reputation:

Yes, in a data step you would use something like this mean(OF d1-d100). Note the of inside the function. It is a symbolic reference saying calculate mean of d1, d2, d3, ..., d100 columns

Upvotes: 3

Related Questions