Solved
New Contributor
Posts: 3

I like to transponse the following dataset called student, which works so far.

input n ID Year\$ rate;

datalines;
1 1 2010 1
2 1 2011 2
3 1 2012 1
4 2 2010 4
5 2 2011 3
6 2 2012 2
7 3 2012 2

with the procedure

proc transponse data=student; out=Zeugnis (drop= _NAME_) ;
by ID;
var rate;
id Year;
run;

Result looks like this.

obs         ID   _2010     _2011  _2012

1              1      1              2          1

2              2      4              3          2

3              3      .              .            2

Now I would like to add the Average colum to the end the display the average rate for each ID.

ID   _2010     _2011  _2012     Average

1       1      1              2          1            1.66

2       2      4              3          2             3

3       3      .              .            2             2

Could you sugest a solution for this, if possible a simple line in the transponse procedure?

I tried to add this line, but it doies not work:   AVERAGE=mean(rate);

Thanks!

Accepted Solutions
Solution
‎02-23-2017 10:58 AM
Super User
Posts: 9,599

## Re: Transponse and add average - a solution, but maybe not the most effective!?!

@Ksharp has already provided with the answer, here is the cod ewith your test data.  Note that this "Excel" thinking of having things across the page (and putting "data" as the column names) is the reason your having trouble and willmake your coding harder.  If you know SQL programming you should be competant in working with normalised data and the only time you would transpose is if an output report is needed like that.

```data studentrates;
input x ID Year rate ;
datalines;
1 1 2010 1
2 1 2011 2
3 1 2012 1
4 2 2010 4
5 2 2011 3
6 2 2012 2
7 3 2012 2
8 1 2013 1
;
run;

proc sort data=studentrates;
by id year;
run;

proc transpose data=studentrates out=want;
by id;
var rate;
id year;
idlabel year;
run;

data want;
set want (drop=_name_);
avg=mean(of _:);
run;```

All Replies
Super User
Posts: 9,599

## Re: Transponse and add average

Add a row to your original dataset with the average, you can do that in many ways, then transpose the dataset up.  Quite straightforward.  I would advise against going with the transposed idea however, unless its for an output report as that will make all your coding form there on harder.

New Contributor
Posts: 3

## Re: Transponse and add average

Supposed, that this is the output and the source is a linked file:

would you also recomend to make a copy with the average first and then run the transponse?

Is it only hard or simply inpossible to make calculations in the Transponse proc?

Super User
Posts: 9,599

## Re: Transponse and add average

I don't know what this means; "Supposed, that this is the output and the source is a linked file:"

Anyways, you can do things on transposed data with arrays, its just more effort:

```data want;
set have;
array vals{3} _2010 _2011 _2012;
av=mean(of vals{*});
run;```

Save yourself a lot of coding however and ditch the "Excel" way of thinking.

Super User
Posts: 10,770

## Re: Transponse and add average

``````data x;
input obs         ID   _2010     _2011  _2012 ;
mean=mean(of _:);
cards;
1              1      1              2          1
2              2      4              3          2
3              3      .              .            2
;
run;``````
New Contributor
Posts: 3

## Re: Transponse and add average - a solution, but maybe not the most effective!?!

Now I got what I need, however I feel that I have choosen an very ineffective way.

After creating asn sortin the main data table "studentrates"

-I sort

- Transpones

- Calculate the mean and store that result into data set

-merge the Transponse with the mean

I feel that this is very time consuming, at least compared with  Excel/SQL. I would appreciate a suggestion how to mak this smaler and faster.

*code;

data studentrates;
input x ID Year rate ;
datalines;
1 1 2010 1
2 1 2011 2
3 1 2012 1
4 2 2010 4
5 2 2011 3
6 2 2012 2
7 3 2012 2
8 1 2013 1
;
run;

proc sort data =studentrates;
by ID;
run;

*create a transponsed table of all students rates;
proc transponse data=studentrates out=Trans_rates (drop= _NAME_) ;
by ID;
var rate;
id Year;
run;

*calculate means and store them in data set;
ods exclude all;
proc means data= studentrates mean;
class ID;
var rate ;
ods output Summary=rates_mean;
run;
ods exclude none;

data Merge_Trans_rates;
merge Trans_rates (in=a) rates_mean (in=b);
by id;
run;
proc print data=Merge_Trans_rates;
run;

Solution
‎02-23-2017 10:58 AM
Super User
Posts: 9,599

## Re: Transponse and add average - a solution, but maybe not the most effective!?!

@Ksharp has already provided with the answer, here is the cod ewith your test data.  Note that this "Excel" thinking of having things across the page (and putting "data" as the column names) is the reason your having trouble and willmake your coding harder.  If you know SQL programming you should be competant in working with normalised data and the only time you would transpose is if an output report is needed like that.

```data studentrates;
input x ID Year rate ;
datalines;
1 1 2010 1
2 1 2011 2
3 1 2012 1
4 2 2010 4
5 2 2011 3
6 2 2012 2
7 3 2012 2
8 1 2013 1
;
run;

proc sort data=studentrates;
by id year;
run;

proc transpose data=studentrates out=want;
by id;
var rate;
id year;
idlabel year;
run;

data want;
set want (drop=_name_);
avg=mean(of _:);
run;```
Valued Guide
Posts: 505

## Re: Transponse and add average - a solution, but maybe not the most effective!?!

``````data have;
input x ID Year rate ;
datalines;
1 1 2010 1
2 1 2011 2
3 1 2012 1
4 2 2010 4
5 2 2011 3
6 2 2012 2
7 3 2012 2
8 1 2013 1
;
run;

Ods Output Observed=havsum(Rename=(Label=Product sum=Total));
Proc Corresp Data=have Observed dim=1;
Table id, year;
weight rate;
run;quit;

data want;
set havsum;
avg=mean(of _:);
run;quit;

Up to 40 obs WORK.WANT total obs=4

Obs    PRODUCT    _2010    _2011    _2012    _2013    TOTAL     AVG

1       1          1        2        1        1         5     1.25
2       2          4        3        2        0         9     2.25
3       3          0        0        2        0         2     0.50
4       Sum        5        5        5        1        16     4.00

``````
☑ This topic is solved.