BookmarkSubscribeRSS Feed
rajeshalwayswel
Pyrite | Level 9

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..

9 REPLIES 9
SuryaKiran
Meteorite | Level 14

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
Ksharp
Super User

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;
novinosrin
Tourmaline | Level 20
 
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;
rajeshalwayswel
Pyrite | Level 9
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

novinosrin
Tourmaline | Level 20

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;

 

 

rajeshalwayswel
Pyrite | Level 9

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....

 

 

 

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User

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;
rajeshalwayswel
Pyrite | Level 9

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.....

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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