BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
Hi All,
I have a small question.
Say I need to find the count of distinct values for each of the following in a dataset that has
more than a million records for my analysis:
complaint_id
indicator
dataentry_date
assignment_reassignment_date
complainant_type
contract_id
complaint_cat_id
category_description
complaint_subcat_id
subcategory_description

proc sql;
select count(distinct compliant_id) from table1;
quit;

I will have to run this multiple times and it will take time.I'm wondering if there is any way i can accomplish this in one step?
3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12
One way that only passes the big dataset once is to use PROC FREQ and output the one-way frequencies. You'll need to use one TABLES statement (with the OUT= option) for each variable. The record count for each output dataset is the number of unique values for each variable. Not elegant, but effective and reasonably efficient.

Doc Muhlbaier
Duke
LinusH
Tourmaline | Level 20
If you like you can use SQL as well, just add a count distinct for each of your columns. Not sure though how PROC SQL will process this internally regarding to efficiency.

/Linus
Data never sleeps
MikeRhoads
Obsidian | Level 7
I just ran into this thread while looking for something else. Just for the record (almost certainly too late for the original poster), there is a very easy way to do this -- the NLEVELS option of PROC FREQ:

ods listing close;
ods output NLevels=CountOfDistinctValues;

proc freq data=sashelp.class nlevels;
tables _all_;

run;
ods output close;
ods listing;

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1211 views
  • 0 likes
  • 4 in conversation