*
;

   options nosource;
   /*Written by Arnold Schick,  University of Marburg/Germany
     using SAS 6.10 under MS Windows 3.11
     tested in SAS 6.09 on AIX Unix
     May 20, 1996

     Note: If you want rows and columns arithmetic mean,
           the macro AMEAN should be applied in the order:
                1th call:  ROW as ASPECT parameter
                2nd call:  COL as ASPECT parameter

           Characters are excluded, however allowed.

   */

%macro amean (data, aspect, result);
 options nomprint nonotes nosymbolgen;

 %if %upcase(&data) = HELP
 %then %do;
   %put;
   %put note: input data set name was &data, sorry, giving help information:;
   %put;
   %put This macro AMEAN computes the arithmetic mean;
   %put from columns or rows in a SAS data set;
   %put;
   %put Macro Parameter Specification: ;
   %put;
   %put DATA   = input data set name, default is HELP;
   %put ASPECT = specifies ROW or COLUMN for the computed mean, default is ROW;
   %put RESULT = output data set name, default is _NEW_;
   %put;
   %goto next;
 %end;

 %if &result =  %then %let result = _NEW_;
 data _NULL_;
   if 0 then set &data nobs=n;
   call symput ('n',n);
   stop;
 run;

%if &n=0 %then %goto quit;

 %if %upcase(&aspect)=COL    or
     %upcase(&aspect)=COLUMN or
     %upcase(&aspect)=COLUMMS %then %let aspect=column;
 %else %let aspect=row;

 %if &aspect=column
  %then %do;
     proc transpose data=&data out=_temp_;
     run;
  %end;
  %else %do;
     proc transpose data=&data out=_temp_;
     run;
     data _ident_;
       set _temp_ end=last nobs=n;
       length _name_ $8.;
       keep _name_ number;
       number = _N_;
       if last then do;
         call symput ('n',n);
         output;
         _name_ = 'AMEAN';
         number +1;
       end;
       output;
     run;
     data _temp_;
        set _temp_;
        drop _name_;
     run;
     proc transpose data=_temp_ out=_temp_;
     run;
  %end;
%let n=&n;

data &result;
   set _temp_ end=last;
   length amean 8 sum 8 ;
   drop i sum;
   array x(*) col1 - col&n ;
   sum = 0 ;
   do i = 1 to dim(x) ;
      if x(i) ne . then sum + x(i) ;
   end ;
   amean = sum / &n;
run;

%if &aspect=column
 %then %do;
   proc transpose data=&result out=&result;
   run;
   options notes;
   data &result;
     set &result;
     if _name_ ^= 'AMEAN' then _name_ = 'ROW' || substr(_NAME_,4,4);
   run;
   options nonotes;
   proc datasets nolist;
     delete _temp_ ;
   quit;
 %end;
 %else %do;
   data &result;
     set &result;
     drop _name_;
   run;

   proc transpose data=&result out=&result;
   run;

   data &result;
     set &result;
     drop _name_;
     number = _N_;
   run;

   data &result;
     update &result _ident_;
     by number;
   run;

   proc transpose data=&result out=&result;
   run;

   options notes;
   data &result;
     set &result end=last;
     drop _name_ ;
     if last then delete;
   run;
   options nonotes;

   proc datasets nolist;
     delete _temp_   _ident_;
   quit;
%end;

%goto next;
 options notes;
%quit : %put data set &data is empty;
%next : ;
 options notes;
%mend;      options source;

*Example;
*to run, please remove comment brackets;
/*
%amean(help);

data eins;
  length mm $2.;
  do a1=1 to 10;
     a2=a1*a1; a3=a1/a2; mm='jj'; a5=a1+a3; b7=a1-a3;
     umsatz=a1+a5-b7; nnn=umsatz-a2;
     output;
  end;
run;

%amean(eins, row, res1);
%amean(res1, col, res2);

 proc print data=res1; run;
 proc print data=res2; run;

 */

*
;