## How to add the average value for each ID into the dataset

Super Contributor
Posts: 323

# How to add the average value for each ID into the dataset

[ Edited ]

Hi

I have data set with four    variables;           id     test      seq     value

data&colon;                                                           1     DBP       1         88

1     DBP       2        86

1     DBP       3          .

1     DBP       1         90

1     DBP       2        86

1     DBP       3         88

1     DBP      4         84

1    SBP       1         128

1     SBP       2        130

I need to calculate average by id and all the sequence values and include in my data

My output should look like:

id     test      seq     value          Type

1     DBP       1         88

1     DBP       2        88

1     DBP       3          .

1      DBP                 88             average

1     DBP       1         90

1     DBP       2        86

1     DBP       3         88

1     DBP      4         84

1      DBP                 87             average

1    SBP       1         128

1     SBP       2        130

1      SBP                 129              average

my code is:

proc transpose data=one out=two(drop=_name_);
by test;
var value;
id seq;
run;

data three;
set two;
four=mean(_1,_2,_3,_4);
run;

proc transpose data=three out=four;
var _1 _2 _3 _4 four;
by test;
run;

I am getting expected results. But I want to know any other simple appraoch. Please advise.

Thanks

Super User
Posts: 23,776

## Re: How to add the average value for each ID into the dataset

[ Edited ]

I don't know if it's simpler but you could use a DOW loop.

You could also use proc means and append results instead of transpose.

Super Contributor
Posts: 323

## Re: How to add the average value for each ID into the dataset

what is the code for Proc means and append the results. Thank you

Super User
Posts: 23,776

## Re: How to add the average value for each ID into the dataset

I highly highly recommend learning proc means.

You'll need to sort the results to get what you want but the following should get you started. It's obviously only a sketch of the code, again I highly recommend learning the proc. Here's a paper and you'll find many many more on lexjansen.com

http://www.lexjansen.com/nesug/nesug08/ff/ff06.pdf

``````proc means data=have;
by GROUPING_VARIABLE;
var ANALYSIS_VARIABLE;
output out=avg_value mean=NAME;
run;

data want;
set have avg_value;
run;

``````

Super User
Posts: 5,888