Contributor
Posts: 25

# how to calculate average

Hi All,

I need to take an average of samples1 to 10 for respective treatment and replication and then would like to create a new variable of a treatment average (I have already sorted the data for replications and treatments). How can I achieve this by SAS coding, please?. It looks straight forward in excel but I am new to SAS and would  like to learn it. Thank you in advance.

 Replication (1-3) Treatment (1-18) Sample1-10 1 1 values 2 2 3 3

Super User
Posts: 23,724

## Re: how to calculate average

Do you have ten variables, Sample1-Sample10 that you need an average for each line? Or are you calculating the average using data from multiple rows?

If 10 variables

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245914.htm

If multiple rows

http://support.sas.com/training/tutorial/studio/summary-statistics.html

It also depends on what you want for output, which you haven't specified.

Posts: 1,337

## Re: how to calculate average

Assuming your sample variables are named SAMPLE1, SAMPLE2, .... SAMPLE9 SAMPLE10, then

data want;

set have;

sample_average=mean(of sample1-sample10);

run;

Contributor
Posts: 25

## Re: how to calculate average

Hi,

I used the codes you suggested. It is not working. Here is the error I get:
ERROR 180-322: Statement is not valid or it is used out of proper order.

My program code is:
data yield; set farmsys.exp38;
proc sort data=yield; by Rep Trt YLD_P1 - YLD_P10;
Avg = mean(OF YLD_P1 - YLD_P10);
proc print data=yield;
run;

that is how the data is tabulated:

Rep

Plot

Trt

YLD_P1

YLD_P2

YLD_P3

YLD_P4

YLD_P5

YLD_P6

YLD_P7

YLD_P8

YLD_P9

YLD_P10

R1

101

15

197.3

53.3

202.3

171.6

202.8

200.5

122.4

190.8

190.1

180.9

R1

102

10

131.4

99.5

135.5

191.5

.

151.4

177.4

194.3

187.9

94.5

Thank you very much for your input on this.

fridge

Posts: 1,337

## Re: how to calculate average

Data transfomations can generally only be done in a DATA step or PROC SQL.  DefinitelyNOT in a proc sort.

SAS is a procedure-oriented tool (some call it a language).

So ...

• Move the "avg=" statement after the SET statement.
• And termnate the data step (and the proc sort step) with a "run;" statement.
Not required, but good practice

Then read something like "The Little SAS Book" to get the basics.  (and run some SAS tutorials.).

Discussion stats
• 4 replies
• 4603 views
• 2 likes
• 3 in conversation