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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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

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