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

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!

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