/**********************************************************************/

   /********************************************************/
   /*                                                      */
   /* The following numeric variables and their values     */
   /* from the SASUSER.HOUSES data set are used in the     */
   /* example:                                             */
   /*                                                      */                                             */
   /*         SASUSER.HOUSES                               */
   /* SQFEET  BEDROOMS  BATHS  PRICE                       */
   /* 1250       2      1.0   64000                        */
   /* 1190       1      1.0   65850                        */
   /* 1400       2      1.5   80050                        */
   /* 1810       4      3.0  107250                        */
   /* 1500       3      3.0   86650                        */
   /* 1615       4      3.0   94450                        */
   /* 1305       3      1.5   73650                        */
   /* 1390       3      2.5   79350                        */
   /* 1040       2      1.0   55850                        */
   /* 2105       4      2.5  127150                        */
   /* 1535       3      3.0   89100                        */
   /* 1240       2      1.0   69250                        */
   /*  720       1      1.0   34550                        */
   /* 1745       4      2.5  102950                        */
   /* 1860       2      2.0  110700                        */


/**********************************************************************/


   /* create an output data set and name the statistic     */
   /* result by which to order                             */
proc means data=sasuser.houses noprint;
   output out=temp mean=;

   /* transpose the data set so that each variable         */
   /* name becomes the value of _NAME_ and each mean       */
   /* the value of COL1                                    */
proc transpose data=temp(drop=_freq_ _type_) out=temp;

   /* sort the data set so the means are in                */
   /* descending order                                     */
proc sort data=temp out=temp;
   by descending col1;

   /* create a unique macro variable for each observation  */
   /* to record the order in which the variables should    */
   /* appear                                               */
data _null_;
   set temp end=last;
   call symput('v'||left(put(_n_,3.)),_name_);
   if last then call symput('max',put(_n_,3.));
run;

   /* use macro to rewrite the PROC MEANS step using a     */
   /* VAR statement with the customized order of the       */
   /* variables and display the final ordering sequence    */
%macro gencode;
   options nonumber nodate nolabel;
   proc means data=sasuser.houses mean std maxdec=0;
      title "FINAL ORDERING SEQUENCE";
      var
         %do i=1 %to &max;
            &&v&i
         %end;
      ;
   run;
%mend gencode;

   /* invoke the macro                                     */
%gencode


/**********************************************************************/


proc means data=sasuser.houses mean maxdec=0;
   title "final ordering sequence";
   var price sqfeet bedrooms baths;
run;


/**********************************************************************/


   /* transpose SASUSER.HOUSES so that each variable       */
   /* name is the value of _NAME_ and each numeric value   */
   /* is the value COL1.  This is accomplished using the   */
   /* BY statement to transpose each observation           */
   /* individually.                                        */
proc transpose data=sasuser.houses
               out=temp2(keep=_name_ col1);
   by notsorted street;
   var _numeric_;

   /* Generate the final ordering sequence by using        */
   /* PROC SQL to first calculate the desired statistics   */
   /* and then order by the descending values of the mean  */
   /* statistic                                            */
options label;

proc sql;
   select _name_ label='Variable',
          mean(col1) as mean label='Mean'    format=12.,
          std(col1)  as std  label='Std Dev' format=12.
   from temp2
   group _name_
   order by mean desc;
run;


/**********************************************************************/