## I have data set need to count column b values how many times it repeated in column a?

Frequent Contributor
Posts: 85

# I have data set need to count column b values how many times it repeated in column a?

```data a;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4
4
;
run;

o/p:

b c
1 4
2 1
3 1
4 2
5 0
6 0

```

I have data need to count b column values with in a column how many times it repeated and b columns values must be equal to a as well as it should be less then of it..

Valued Guide
Posts: 558

## Re: I have data set need to count column b values how many times it repeated in column a?

If you don't have very large data then you can perform a Cartesian product and write a logic to count.

``````data a;
infile cards missover;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4
4
;
run;

proc sql;
create table many as
select t1.b,t2.a
from a as t1,a as t2
where t1.b is not null
order by t1.b
;
quit;
data want(drop=a);
retain count;
set many;
by b;
if first.b then count=0;
If b-1<a<=b then count+1;
if last.b;
run;``````

If your data is large I suggest Hash table method.

Thanks,
Suryakiran
Super User
Posts: 10,681

## Re: I have data set need to count column b values how many times it repeated in column a?

``````
data a;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4
4
;
run;

proc sql;
select a.b,sum(a.b=b.a) as count
from a as a,a as b
where a.b is not missing
group by a.b;
quit;``````
PROC Star
Posts: 1,558

## Re: I have data set need to count column b values how many times it repeated in column a?

``````
data a;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4	.
4	.
;
run;
proc sql;
create table want as
select a.b,coalesce(c,0) as count
from a a left join (select a,count(a) as c from a group by a) b
on a.b=b.a
where a.b ne .;
quit;``````
Frequent Contributor
Posts: 85

## Re: I have data set need to count column b values how many times it repeated in column a?

 0.2556% 0.2875% 0.2559% 0.3812% 0.2564% 0.4748% 0.2667% 0.5684% 0.2701% 0.6620% 0.2875% 0.7556% 0.2880% 0.8493% 0.3138% 0.9429% 0.3196% 1.0365% 0.3198% 1.1301% 0.3213% 1.2237% 0.3226% 1.3174% 0.3262% 1.4110% 0.3318% 1.5046% 0.3447% 1.5982% 0.3508% 1.6919% 0.3513% 1.7855% 0.3526% 1.8791% 0.3527% 1.9727% 0.3536% 2.0663% 0.3545% 2.1600% 0.3632% 2.2536% 0.3706% 2.3472%

some more clear on want I want thanks alot everyone ..but I need like B column value need to search for same and less then to A column value.. if it taken a count from A column it doesn't repeat count when it taken a new value in column B....example

I'm showing how it on small data to make clear...

a       b

1.1    1

1.2     2
1.3     3

2

2.1

2.2

3

Require output:

b c

1 0

2 4

3 1

PROC Star
Posts: 1,558

## Re: I have data set need to count column b values how many times it repeated in column a?

[ Edited ]

Are you after this????

Your wrote --" I need like B column value need to search for same and less then to A column value."

Require output:

b c

1 0

2 4

3 1 /*this should be 7?*/

``````data a;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4	.
4	.
;
run;
proc sql;
create table want as
select a.b,sum(b.a<=a.b) as count
from a a,a b
where  a.b ne .
group by a.b;
quit;``````

Frequent Contributor
Posts: 85

## Re: I have data set need to count column b values how many times it repeated in column a?

In the below which you written code the count gives as

b  count

1     4

2     5

3     6

4     8

5      8

6       8

b  count

1     4

2     1

3     1

4     2

5      0

6      0

In this way needed ... if a value is counted when it less then or equal to.. it should not repeat count again....

Posts: 1,309

## Re: I have data set need to count column b values how many times it repeated in column a?

Since both columns a and b are sorted, you can take advantage of that fact as below:

``````data a;
infile datalines missover;
input a b;
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4
4
run;

data want ;
do c=0 by 1 until (inb);
set a (keep=a rename=(a=b) in=ina)
a (keep=b where=(b^=.) in=inb);
by b;
end;
run;
``````
Super User
Posts: 10,681

## Re: I have data set need to count column b values how many times it repeated in column a?

``````
data a;
input _a b;
a=ceil(_a);
cards;
0.1 1
0.2 2
1   3
1   4
2   5
3   6
4
4
;
run;

proc sql;
select a.b,sum(a.b=b.a) as count
from a as a,a as b
where a.b is not missing
group by a.b;
quit;``````
Frequent Contributor
Posts: 85

## Re: I have data set need to count column b values how many times it repeated in column a?

0.00256 0.00288
0.00256 0.00381
0.00256 0.00475
0.00267 0.00568
0.00270 0.00662
0.00288 0.00756
0.00288 0.00849
0.00314 0.00943
0.00320 0.01037
0.00320 0.01130
0.00321 0.01224
0.00323 0.01317
0.00326 0.01411
0.00332 0.01505
0.00345 0.01598
0.00351 0.01692
0.00351 0.01785
0.00353 0.01879
0.00353 0.01973
0.00354 0.02066
0.00354 0.02160
0.00363 0.02254
0.00371 0.02347
0.00378 0.02441
0.00380 0.02534
0.00383 0.02628

Thanks everyone I have applied above code from the following data but count not getting.. can some one check

Required output:

count

6 ---less then or equal to 0.00288

18----less then or equal to 0.00381

and so on.....

Discussion stats
• 9 replies
• 192 views
• 1 like
• 5 in conversation