What is the best way to sort - by descending mean - the output of PROC MEANS with multiple variables?
PROC MEANS DATA=sas_communities MAXDEC=1;
VAR thc_ssx_vas_anx--thc_ssx_vas_other;
RUN;
I tried outputting the summary stats to a new data set, but the output of the PROC PRINT is one long row (See below).
PROC MEANS DATA=sas_communities MAXDEC=1;
VAR
thc_ssx_vas_anx--thc_ssx_vas_other;
OUTPUT OUT = thc_ssx_vas_summary (DROP = _:)
n (thc_ssx_vas_anx--thc_ssx_vas_other) =
mean (thc_ssx_vas_anx--thc_ssx_vas_other) =
std (thc_ssx_vas_anx--thc_ssx_vas_other) =
min (thc_ssx_vas_anx--thc_ssx_vas_other) =
max (thc_ssx_vas_anx--thc_ssx_vas_other) = ;
RUN;
Thank you. Dataset attached.
there are two ways, both described in the article "Save descriptive statistics for multiple variables in a SAS data set."
The one that uses PROC MEANS would look something like this:
ods exclude all; /* suppress display to open ODS destinations */
proc means data=sashelp.cars
N Mean Std Min Q1 MEDIAN Q3 Max /* type every statistic you want */
STACKODSOUTPUT; /* preserve table form of output */
var _NUMERIC_;
ods output Summary=MeansSummary; /* write statistics to data set */
run;
ods exclude none;
proc sort data=MeansSummary;
by descending Mean;
run;
proc print data=MeansSummary noobs;
format _NUMERIC_ BESTD6.;
var Variable N Mean StdDev Min Max;
run;
there are two ways, both described in the article "Save descriptive statistics for multiple variables in a SAS data set."
The one that uses PROC MEANS would look something like this:
ods exclude all; /* suppress display to open ODS destinations */
proc means data=sashelp.cars
N Mean Std Min Q1 MEDIAN Q3 Max /* type every statistic you want */
STACKODSOUTPUT; /* preserve table form of output */
var _NUMERIC_;
ods output Summary=MeansSummary; /* write statistics to data set */
run;
ods exclude none;
proc sort data=MeansSummary;
by descending Mean;
run;
proc print data=MeansSummary noobs;
format _NUMERIC_ BESTD6.;
var Variable N Mean StdDev Min Max;
run;
missed Rick's post while I was away
While the STACKODSOUTPUT solves some needs, a SAS conference paper can be found with a macro named %better_means.
See http://support.sas.com/resources/papers/proceedings12/231-2012.pdf
The macro solves the problem of generating stats of differing data types within a column by converting values to strings in appropriate formats
unfortunately the macro source code isn't short, and not easily extracted from the paper, so I've attached it here
I have a similar problem: how to sort the means of a variable in descending order? My goal is to organize the 2nd table (see output below), sort the table in the order of descending mean, so 965 at the bottom while 979 at the top. I tried the method using proc sort (see my code) but the program didn't recognize Mean as a variable.
Thank you so much for all of your help!
proc means data=pg1.storm_final
N Mean Min maxdec=0;
var MinPressure;
ods output Summary=N_avg_min_Summary;
where Season >=2010;
class Season Ocean;
ways 1;
run;
proc sort data=N_avg_min_Summary;
by descending Mean;
run;
You have to LOOK at the output data set and determine the names of the variables for your data and analysis. For your example, try the following:
proc sort data=N_avg_min_Summary out=SeasonMeans;
where not missing(Season);
by descending MinPressure_Mean;
run;
proc print data=SeasonMeans; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.