I have a hypothetical data as below.
If data in the 1st, 2nd, 3rd, 5th and 6th column are same and 4th column is either "A" or "B" then they will be considered as duplicate and one of those rows will be kept.
also
If data in the 1st, 2nd, 3rd, 5th and 6th column are same and 4th column is either "D" or "E" then they will be considered as duplicate and one of those rows will be kept.
So, the following rows will be considered as duplicates and one of them will be kept.
Rows 1 and 2
Rows 4 and 5
Rows 6 and 7
Rows 9 and 10
data have;
input ID date Test $ SubTest $ SourceCode Result $;
datalines;
1 1/1/21 ABC A 1 Positive
1 1/1/21 ABC B 1 Positive
1 1/1/21 ABC C 1 Negative
1 1/10/21 DEF D 1 Positive
1 1/10/21 DEF E 1 Positive
1 1/10/21 DEF F 1 Negative
2 1/1/21 ABC A 1 Negative
2 1/1/21 ABC B 1 Negative
2 1/1/21 ABC C 1 Negative
2 1/10/21 DEF D 1 Positive
2 1/10/21 DEF E 1 Positive
2 1/10/21 DEF F 1 Negative
3 1/1/21 ABC A 1 Positive
3 1/1/21 ABC B 1 Negative
3 1/1/21 ABC C 1 Negative
3 1/10/21 DEF D 1 Negative
3 1/10/21 DEF E 1 Positive
3 1/10/21 DEF F 1 Negative
4 1/1/21 ABC A 1 Positive
4 1/1/21 ABC B 2 Positive
4 1/1/21 ABC C 1 Negative
4 1/10/21 DEF D 1 Negative
4 1/10/21 DEF E 1 Positive
4 1/10/21 DEF F 1 Negative
;
run;
data want;
set have;
prev_id=lag(id);
prev_date=lag(date);
prev_test=lag(test);
prev_subtest=lag(subtest);
prev_sourcecode=lag(sourcecode);
prev_result=lag(result);
if prev_id=id and prev_date=date and prev_test=test
and prev_sourcecode=sourcecode and prev_result=result then do;
if prev_subtest='A' and subtest='B' then delete;
if prev_subtest='D' and subtest='E' then delete;
if prev_subtest=subtest then delete;
end;
drop prev_:;
run;
data want;
set have;
prev_id=lag(id);
prev_date=lag(date);
prev_test=lag(test);
prev_subtest=lag(subtest);
prev_sourcecode=lag(sourcecode);
prev_result=lag(result);
if prev_id=id and prev_date=date and prev_test=test
and prev_sourcecode=sourcecode and prev_result=result then do;
if prev_subtest='A' and subtest='B' then delete;
if prev_subtest='D' and subtest='E' then delete;
if prev_subtest=subtest then delete;
end;
drop prev_:;
run;
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.