Pyrite | Level 9

## Counting N by Each UNIQUE

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
SAS Super FREQ

## Re: Counting N by Each UNIQUE

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 Super FREQ

## Re: Counting N by Each UNIQUE

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

``````
From The DO Loop