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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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