BookmarkSubscribeRSS Feed
ren2010
Obsidian | Level 7
Hi All,

Here is my requirement;I have two data sets,for example

Dataset A
------------------------------
ID | NAME
-----------------------------
1 john
2 sara
3 tom
Dataset B
--------------------------------
ID | Channelswatched
--------------------------------
1 HBO
1 CNN
1 CBS
1 MTV
2 BBC
2 FOX
3 HBO

i need to add a counter say for example CNTR to capture how many times an id appears in Dataset B and pass to final dataset.
The final dataset should look like the following

Final Dataset
-------------------------------------------------------------
ID | NAME | CNTR | Channelswatched
------------------------------------------------------------
1 john 1 HBO
1 john 2 CNN
1 john 3 CBS
1 john 4 MTV
2 sara 1 BBC
2 sara 2 FOX
3 mary 1 HBO

Please help.

Thanks,
REN
2 REPLIES 2
Patrick
Opal | Level 21
Data A;
input ID NAME $;
datalines;
1 john
2 sara
3 tom
;
Data B;
input ID Channelswatched $;
datalines;
1 HBO
1 CNN
1 CBS
1 MTV
2 BBC
2 FOX
3 HBO
;

data B_counter;
set b;
by id;
if first.id then CNTR=0;
CNTR+1;
run;

proc sql;
select a.*,b.Channelswatched,b.CNTR
from A as a, B_counter as b
where a.id=b.id
order by id,CNTR
;
quit;


HTH
Patrick
ren2010
Obsidian | Level 7
Thanks so much Patrick.
Happy New Year.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1482 views
  • 0 likes
  • 2 in conversation