Help using Base SAS procedures

Need SAS logic to add counter to a dataset.

Reply
Contributor
Posts: 74

Need SAS logic to add counter to a dataset.

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
Respected Advisor
Posts: 3,887

Re: Need SAS logic to add counter to a dataset.

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
Contributor
Posts: 74

Re: Need SAS logic to add counter to a dataset.

Thanks so much Patrick.
Happy New Year.
Ask a Question
Discussion stats
  • 2 replies
  • 96 views
  • 0 likes
  • 2 in conversation