BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Newbe11
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

@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;

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Newbe11
Fluorite | Level 6

Thanks for your advise! 

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? 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

 

Ksharp
Super User
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;
Newbe11
Fluorite | Level 6

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

@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;
rogerjdeangelis
Barite | Level 11
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


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 538 views
  • 3 likes
  • 4 in conversation