BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

UNIQUE picks all unique values, and COUNTUNIQUE counts the number of the unique values. For example,

proc iml;
call randseed(1);
x=randfun(5000,"poi",1000);
ux=unique(x);
cux=countunique(x);
quit;

This code simulates X~Poisson(1000) 5000 times. There are 187 unique values from 889 to 1108. Is there any efficient way to get the number of observations for each unique value? For example, 47 out of 5000 observations are X=1000. I use

do i=1 to cux;
if i=1 then n=sum(x=ux[i]);
else n=n||sum(x=ux[i]);
end;

but am looking for a better one.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Sure, Use CALL TABULATE to get the unique values and the counts in one call:

 

proc iml;
call randseed(1);
x=randfun(5000,"poi",1000);
call tabulate(values, count, x);

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

Sure, Use CALL TABULATE to get the unique values and the counts in one call:

 

proc iml;
call randseed(1);
x=randfun(5000,"poi",1000);
call tabulate(values, count, x);

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 820 views
  • 2 likes
  • 2 in conversation