![]() | ![]() | ![]() | ![]() | ![]() |
This sample shows how to dynamically create an input variable list for PROC MEANS and also shows how to create your own names for the output variables rather than using the AUTONAME option.
If you run MEANS against sashelp.class and choose the AUTONAME option on the OUTPUT statement, the format of the names is variablename_stat where variablename is the actual name of the variable and stat is the statistic(s) you requested. The code below illustrates this.
The sample code in the Full Code tab works if you are requesting only one statistic. In the step where the new variable names are created, you would have to create additional variables for each statistic requested in the PROC MEANS, and add additional select statements to the PROC SQL.
In the example, the first step retrieves and renames all the variables in the input dataset. In this step, the variable names are actually the values associated with the variable NAME from the PROC CONTENTS output dataset and the newly created variable name.
The PROC SQL step creates an input variable list and a list of new variable names for only the numeric variables. Numeric variables have a type = 1.
The final two steps are MEANS and PRINT to produce and display the output.
Note: The CATS function is new for SAS 9 and above. For other SAS releases use the concatenation bars (||) along with the LEFT and TRIM functions.
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.
title;
/* get the list of variable names in the dataset */
proc contents data=sashelp.class out=stuff(keep=name type) noprint;
run;
data temp;
set stuff;
length sumname $14.;
sumname=cats(name,'_total');
run;
/* look at new name */
proc print data=temp;
title 'temp';
run;
title;
/* create macro var with list of numeric variable names */
proc sql;
select (sumname)
into :outname separated by ' '
from temp
where type = 1;
select (name)
into :inname separated by ' '
from temp
where type = 1;
quit;
proc means data=sashelp.class;
var &inname;
output out=mymeans sum= &outname;
run;
/* look at final output */
proc print data=mymeans;
title 'mymeans';
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.
mymeans 09:10 Wednesday, January 21, 2009 195
Age_ Height_ Weight_
Obs _TYPE_ _FREQ_ total total total
1 0 19 253 1184.4 1900.5
| Type: | Sample |
| Topic: | SAS Reference ==> Procedures ==> MEANS SAS Reference ==> Macro SAS Reference ==> SQL |
| Date Modified: | 2009-01-22 14:12:18 |
| Date Created: | 2009-01-22 11:17:50 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | z/OS | ||
| 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 XP Professional | ||||
| Windows Millennium Edition (Me) | ||||
| Windows Vista | ||||
| 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 | ||||




