Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the mean value of columns in SAS

Tags:

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?

like image 240
user3311403 Avatar asked Dec 12 '22 07:12

user3311403


2 Answers

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.

like image 194
pteranodon Avatar answered Dec 13 '22 20:12

pteranodon


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;
like image 39
Longfish Avatar answered Dec 13 '22 20:12

Longfish