Sample 36898: Count the distinct values of a variable
The sample code on the Full Code tab illustrates how to count the distinct values of a variable.
The question of how to count distinct values of a CLASS or BY variable using either PROC MEANS or PROC SUMMARY is asked frequently. While neither of these procedures has this ability, PROC SQL can count these values using the DISTINCT option or PROC FREQ can be used with the NLEVELS option.
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.
This example creates an output data set using PROC SQL whose variables contain the distinct counts. You can also use PROC FREQ to create an output table with the distinct counts.
/* Use the NLEVELS option with the ODS SELECT statement to capture the */
/* number of levels for a variable. */
ods select nlevels;
proc freq data=sashelp.class nlevels;
tables name age sex;
title 'Number of distinct values for each variable';
run;
/* Use PROC SQL to count the number of levels for a variable. */
proc sql;
create table new as
select count(distinct(name)) as namecount,
count(distinct(age)) as agecount,
count(distinct(sex)) as sexcount
from sashelp.class;
quit;
proc print;
title 'Number of distinct values for each variable';
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.
Number of distinct values for each variable 12
The FREQ Procedure
Number of Variable Levels
Variable Levels
Name 19
Age 6
Sex 2
Number of distinct values for each variable
Obs namecount agecount sexcount
1 19 6 2
This sample shows you how you can use PROC FREQ with the NLEVELS option or PROC SQL to count the distinct values of variables.
Date Modified: | 2009-09-03 13:44:40 |
Date Created: | 2009-08-17 14:18:45 |
Operating System and Release Information
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 Server 2008 | | |
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 | | |