BookmarkSubscribeRSS Feed
PoojaP1
Fluorite | Level 6

Hello,

 

Could anyone help, on how to count distinct values for a character type field in a SAS dataset? Like, I have a field of email addresses in a dataset, and I want to count the distinct email addresses only. I could get the desired results using PROC SQL, but I wanted to know if there is any other way to do the same.

 

Thanks in advance!

7 REPLIES 7
PoojaP1
Fluorite | Level 6

Thanks for your help.

Could you please give a small example for this?

 

Thank you!

LinusH
Tourmaline | Level 20

With the help of the links provided, try to use them. If you run into obstacles, then get back to us.

Data never sleeps
PoojaP1
Fluorite | Level 6

Proc means- does not give any results for Character type variable.

 

Proc freq- I did not get the desired output. Like, I have a field of email address in the dataset, using proc freq displayed all email addresses with count as 1 in front of each.

 

Proc summary- I have never used, I am trying and it runs into errors.

 

 

FreelanceReinh
Jade | Level 19

Hello @PoojaP1,

 

PROC SORT with NODUPKEY and OUT= options would be another method (in addition to the approaches listed by RW9).

 

Please note that some approaches differ in how missing values (and special missing values) are handled. For example, a COUNT(DISTINCT ...) in PROC SQL excludes missing values, whereas one missing value (of each kind in case of special missing values), if any, would remain after a NODUPKEY sort.

 

The optimal method will depend on the number of observations in the dataset, the expected number of distinct values and other considerations, e.g., whether the dataset is already sorted by that character variable.

BrunoSilva
Quartz | Level 8

Hello,

 

Another technique is using Hash tables and SUMINC

 

http://www.pharmasug.org/proceedings/2014/CC/PharmaSUG-2014-CC02.pdf

 

Best regards,

Ksharp
Super User
1)

proc freq data=sashelp.class nlevels;
 table sex;
run;



2) data _null_; if 0 then set sashelp.class; declare hash h(dataset:'sashelp.class'); h.definekey('sex'); h.definedone(); nlevels=h.num_items; putlog "NOTE:" nlevels; stop; run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 4439 views
  • 1 like
  • 6 in conversation