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);

sas-innovate-white.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!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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