Hi there, I am trying to flag duplicate values in my data set.
I have this:
ID | BirthDate |
1 | 12/07/95 |
1 | 12/07/95 |
1 | 12/07/95 |
2 | 12/24/99 |
2 | 12/24/99 |
3 | 07/26/98 |
4 | 09/29/82 |
and I want this:
ID | BirthDate | Recurrent |
1 | 12/07/95 | 1 |
1 | 12/07/95 | 2 |
1 | 12/07/95 | 3 |
2 | 12/24/99 | 1 |
2 | 12/24/99 | 2 |
3 | 07/26/98 | 1 |
4 | 09/29/82 | 1 |
I found the following code in the communities forum but I cannot seem to make it work... Any help is appreciated.
data want;
count=0;
do until (last.BirthDate);
set have;
by ID BirthDate;
if first.BirthDate then count + 1;
end;
if count > 1 then Recurrent=1; else Recurrent=0;
do until (last.BirthDate);
set temp_names;
by ID BirthDate;
output;
end;
run;
data have;
input (ID BirthDate) (:$15.);
cards;
1 12/7/1995
1 12/7/1995
1 12/7/1995
2 12/24/1999
2 12/24/1999
3 7/26/1998
4 9/29/1982
;
data want;
set have;
by id birthdate;
if first.BirthDate then Recurrent=1;
else Recurrent+1;
run;
ID | BirthDate | Recurrent |
---|---|---|
1 | 12/7/1995 | 1 |
1 | 12/7/1995 | 2 |
1 | 12/7/1995 | 3 |
2 | 12/24/1999 | 1 |
2 | 12/24/1999 | 2 |
3 | 7/26/1998 | 1 |
4 | 9/29/1982 | 1 |
data have;
input (ID BirthDate) (:$15.);
cards;
1 12/7/1995
1 12/7/1995
1 12/7/1995
2 12/24/1999
2 12/24/1999
3 7/26/1998
4 9/29/1982
;
data want;
set have;
by id birthdate;
if first.BirthDate then Recurrent=1;
else Recurrent+1;
run;
ID | BirthDate | Recurrent |
---|---|---|
1 | 12/7/1995 | 1 |
1 | 12/7/1995 | 2 |
1 | 12/7/1995 | 3 |
2 | 12/24/1999 | 1 |
2 | 12/24/1999 | 2 |
3 | 7/26/1998 | 1 |
4 | 9/29/1982 | 1 |
Thank you so much. I did not realize it was this simple!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.