Miffy
Miffy

Reputation: 1

count the total number of duplicates for each duplicated observation in SAS

I have the following data; basically, some patients have more than one record.

....

I would like to have a variable "count" which is the total number of duplicates. like below

enter image description here

but code I have now and the results:

data grouped;
    set have;
    by id notsorted;
    retain count;
    if first.id then count=1;
    else count+1;
    run;

enter image description here

Upvotes: 0

Views: 872

Answers (1)

Reeza
Reeza

Reputation: 21264

This add a count total to each record. In this case, SQL is better here.

proc sql;
create table grouped as 
select *, count(*) as count
from have
group by id;
quit;

Upvotes: 1

Related Questions