Removing duplicates based on 2 columns in SAS

Hi, How do I remove duplicates based on 2 columns in SAS?
Who is Participating?
d507201Connect With a Mentor Database Marketing ConsultantCommented:
This approach will create a data set with no duplicates--it will contain only unique records.  Where there are duplicates the record with the youngest age will be retained.  If you want the oldest age then change the BY statement to by ID descending Age.  If you want to capture the duplicate records in a data set then use the dupout= option.

  proc sort data=test ; by ID AGE ; run ;
  proc sort data=test nodupkey ; by ID AGE ; run ;

If you want to create a data set that contain only records that are unique to begin with--there is no conflicting age data--then try this approach.   This code will keep only IDs that have a single value for age-- they'll go into noQuestionAboutAge.  All other records will go into the questionableAge data set.  For example, both records id=100 age=18 and id=100 age=19 would be output to the questionableAge data set.

     proc sort data=test ;
        by id;
      run ;

     data noQuestionAboutAge   questionableAge;   set test;
      by id;
      if and then output noQuestionAboutAge;
      else output questionableAge;

first. and last. are temporary variables created when a DATA step is processed with BY groups.  I use it most often to control merging but it's also very useful for things like this and doing summarization in a DATA step.
bradanelsonConnect With a Mentor Commented:
Here is an example on how to remove dups by 2 fields.

    BY Field1 Field2;
autumnwingsConnect With a Mentor Commented:
You are going to want to first sort your data set by the two variables you'd like to use as your key. Make sure you sort them in ascending/decending order as appropriate because the record that will be kept is the first one after you have sorted.

Then you will run a proc sort again and this time you will add the option 'nodupkey'. This option will only keep the first record in your dataset and eliminiate duplicates.

proc sort data=test ; by ID AGE ; run ;
proc sort data=test nodupkey ; by ID AGE ; run ;
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

WonderwallAuthor Commented:
I did this
proc sort data=test ; by ID AGE ; run ;
proc sort data=test nodupkey ; by ID AGE ; run ;
and it got rid of the duplicates for the first record only, all the rest were kept
It should have deleted any duplicates of the combination of ID and AGE, are you sure it didn't work?
WonderwallAuthor Commented:
yes, I looked at my output and it only worked on the first record
Can you provide sample data to help troubleshoot your issue.
theartfuldazzlerConnect With a Mentor Commented:

An easy code to remove all duplicates is:

  create table new_table as
  select * from old_table
 group by field1, field2
 having count(*) = 1;
WonderwallConnect With a Mentor Author Commented:
Thank you all for your help, finally found out that SAS stores dates as integers.  hmmm, problem fixed
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.