Solved
Contributor
Posts: 26

# How to find frequency of i in each bin of data?

Hi i have data with two variables one is score and other is binary variable "label".

score    label

245       1

261       0

250       1

300       1

270       0

I am interested to find counts of 1 in label variable for the given manual bins. I have written code but it is giving only frequency of squares in the bins as.

``````proc iml;
use work.prtf_data;
read all var {Score} into x;
close work.prtf_data;

cutpts = {.M 261 273 283 291 298 305 312 330 341 342 .I};
r = bin(x, cutpts);

call tabulate(BinNumber, Freq, r);
lbls = {"< 261" "262-273" "274-283" "284-291" "292-298" "299-305" "306-312" "313-330" "331-341" "> 342"};
print Freq[colname=lbls];``````

i want my results as

"score"      "" 1 freq"

<261            2

262-273       0

.....

....

Please suggest me any solution. Thanks

Accepted Solutions
Solution
‎05-15-2017 01:57 AM
Super User
Posts: 10,695

## Re: How to find frequency of i in each bin of data?

``````data class;
input score    label;
cards;
245       1
261       0
250       1
300       1
270       0
200       0
;
run;
proc iml;
use class;
read all var {score label} ;
close ;

cutpts = {.M 262 .I};
r = bin(score, cutpts);

do i=1 to max(r);
idx=loc(r=i);

group=group//i;
freq=freq//sum(label[idx]);
end;

lbls = t( {"< 261" "262-273"} );
print lbls group freq;
quit;``````

All Replies
SAS Super FREQ
Posts: 4,175

## Re: How to find frequency of i in each bin of data?

[ Edited ]

So you only want to count the data for which label=1?  If so, use a WHERE clause to subset the data when you read it in:

``````proc iml;
use work.prtf_data where(label=1);
read all var {Score} into x;
close work.prtf_data;
``````

I can't tell from your example if the LABEL variable is numeric or character. If it is character, then use

WHERE(label='1')

For the general question "how do I count the number of observations in uneven bins," see "Bin observations by using custom cut points and unevenly spaced bins."

Contributor
Posts: 26

## Re: How to find frequency of i in each bin of data?

Hi Rick your solution also worked for me. Thanks
Solution
‎05-15-2017 01:57 AM
Super User
Posts: 10,695

## Re: How to find frequency of i in each bin of data?

``````data class;
input score    label;
cards;
245       1
261       0
250       1
300       1
270       0
200       0
;
run;
proc iml;
use class;
read all var {score label} ;
close ;

cutpts = {.M 262 .I};
r = bin(score, cutpts);

do i=1 to max(r);
idx=loc(r=i);

group=group//i;
freq=freq//sum(label[idx]);
end;

lbls = t( {"< 261" "262-273"} );
print lbls group freq;
quit;``````
☑ This topic is solved.