BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Barkat
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
Barkat
Pyrite | Level 9
Perfect!!! Thanks so much!!! Highly appreciated!!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 751 views
  • 1 like
  • 2 in conversation