Help using Base SAS procedures

sum and avg horizontally in proc means

Reply
Super Contributor
Posts: 396

sum and avg horizontally in proc means

Hi, is there a way I can show the sum and mean (in proc means) horizontally of a dataset?  Eg.

Have

Type     Var1         Var2

A             3              5

A            5               1

B            2               2

B            4               6

Want

Type       Stat_name          Var1       Var2

A            Sum                     8            6

A            Mean                    4            3

B            Sum                     6            8

B            Mean                    3           4

Thanks

PROC Star
Posts: 7,364

Re: sum and avg horizontally in proc means

Isn't that what proc summary does?  e.g.:

proc summary data=have sum mean nway;

class type;

  var var1 var2;

  output out=want  (drop=_type_ _freq_ rename=(_stat_=stat_name));

run;

Super Contributor
Posts: 396

Re: sum and avg horizontally in proc means

Thanks Art,  this is great but it doesn not provide the sum.  It only shows :  N / MIN / MAX / MEAN / STD

Super Contributor
Posts: 396

Re: sum and avg horizontally in proc means

proc summary data=have sum mean nway;

class type;

  var var1 var2;

  output out=want  sum=mean= / autoname ;

run;

Super User
Super User
Posts: 6,502

Re: sum and avg horizontally in proc means

No, but you can use PROC TRANSPOSE to fix it.

ods output summary=want2;

proc means data=have mean sum nway stackodsoutput;

  class type ;

  var var1-var2 ;

  output out=want ;

run;

proc  transpose data=want2 out=want name=stat_name;

  by type ;

  id variable ;

run;

Super User
Posts: 9,687

Re: sum and avg horizontally in proc means

data have;
input Type $    Var1         Var2 ;
cards;
A             3              5
A            5               1
B            2               2
B            4               6
;
run;
proc sql;
 create table want as
 select type,'Sum' as stat_name length=10,sum(var1) as var1,sum(var2) as var2
  from have
   group by type
 union
 select type,'Mean' as stat_name length=10,mean(var1) as var1,mean(var2) as var2
  from have
   group by type
 order by 1,2 desc;
quit;


Xia Keshan

Ask a Question
Discussion stats
  • 5 replies
  • 589 views
  • 0 likes
  • 4 in conversation