BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
I have data set need to count column b values how many times it repeated in column a data l; input a b; cards; 1 1 2 2 2 3 3 . 3 . 3 . 3 . run; Required output : a b 1 1 2 2 4 3
1 ACCEPTED SOLUTION

Accepted Solutions
srinath3111
Quartz | Level 8

Hi,

 

proc sql;
create table final as
select a,
count(a) as COUNT
from l
group by a;
quit;

 

 

data ds;
set l;
by a b;
keep a count;
retain count 0;
if first.a=1 then count=1;
else count=sum(count,1);
if last.a=1 then output;
run;

Thanks,

srinath

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

Could you PLEASE post your code properly, like this:

data l;
input a b;
cards;
1 1
2 2
2 3
3 .
3 .
3 .
3 .
;
run;

It's not rocket science, really.

Please state if the code above is what you intended.

Patrick
Opal | Level 21

@rajeshalwayswel

Assuming "sure...." means yes, that's how I intended to post my code.

data l;
  input a b;
  cards;
1 1
2 2
2 3
3 .
3 .
3 .
3 .
;
run;

proc sql;
  create table want as
    select a, b, count(*) as cnt
      from l
        group by a,b
  ;
quit;
rajeshalwayswel
Pyrite | Level 9
I have data set need to count column b values how many times it repeated in column a 


data l;
input a b;
cards;
1 1
2 2
2 3
3 .
3 .
3 .
3 .
run;


output :


a b

1 1
2 2
4 3
Kurt_Bremser
Super User

@rajeshalwayswel wrote:
I have data set need to count column b values how many times it repeated in column a 


data l;
input a b;
cards;
1 1
2 2
2 3
3 .
3 .
3 .
3 .
run;


output :


a b

1 1
2 2
4 3

Try this:

proc sql;
create table want as
select count(*) as a, a as b
from l
group by a;
quit;
srinath3111
Quartz | Level 8

Hi,

 

proc sql;
create table final as
select a,
count(a) as COUNT
from l
group by a;
quit;

 

 

data ds;
set l;
by a b;
keep a count;
retain count 0;
if first.a=1 then count=1;
else count=sum(count,1);
if last.a=1 then output;
run;

Thanks,

srinath

novinosrin
Tourmaline | Level 20
data have;
input a b;
cards;
1 1
2 2
2 3
3 .
3 .
3 .
3 .
run;

proc sql;
create table want as
select a,count(a) as b
from have
where a in (select b from have)
group by a;
quit;
rajeshalwayswel
Pyrite | Level 9

Thanks everyone.... From next time I'll post questions in proper way... Sorry for the inconvenience.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2294 views
  • 3 likes
  • 5 in conversation