Sample 42381: Reshape PROC MEANS output when requesting non-default statistics
The default output of PROC MEANS, when no statistics are listed in the OUTPUT statement, is a data set containing a _STAT_ variable and the variables listed in the VAR statement. The output data set contains five records, one for each default statistic N, MIN, MAX, MEAN, and STD.
When statistics are listed in the OUTPUT statement, or statistics other than the default are needed, the structure of the output data set changes. The output data set will contain only one observation and a variable for each analysis variable-statistic combination. This sample provides the steps needed to create a data set that matches the structure of the default output with one observation for each statistic.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Reshape the output from PROC MEANS using PROC TRANSPOSE and the DATA step.
/* Request statistics other than the default in the OUTPUT statement. */
/* Specify the AUTONAME option to create consistent variable names. */
proc means data=sashelp.class noprint;
var age height weight;
output out=means1(drop=_type_ _freq_) n= min= max= mean= std= median= / autoname;
run;
/* Transpose the data so that each statistic becomes its own observation. */
proc transpose data=means1 out=means2;
run;
/* Create new variables that contain the statistic */
/* name and the original variable name. */
data means2;
set means2;
varname = tranwrd(_name_,"_"||scan(_name_,-1,'_'),'');
_STAT_ = scan(_name_,-1,'_');
drop _name_;
run;
proc sort data=means2;
by _STAT_;
run;
/* Transpose the data to get one observation for each */
/* unique statistic name and one variable named for */
/* the original variable in the PROC MEANS step. */
proc transpose data=means2 out=means3(drop=_name_);
by _STAT_;
id varname;
var col1;
run;
proc print data=means3;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs _STAT_ AGE HEIGHT WEIGHT
1 Max 16.0000 72.0000 150.000
2 Mean 13.3158 62.3368 100.026
3 Median 13.0000 62.8000 99.500
4 Min 11.0000 51.3000 50.500
5 N 19.0000 19.0000 19.000
6 StdDev 1.4927 5.1271 22.774
This sample shows how to reshape your data from the default output of PROC MEANS with non-default statistics. The result is a data set containing one record per statistic.
Date Modified: | 2019-09-16 13:13:14 |
Date Created: | 2011-02-14 11:08:23 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
Z64 | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |