• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS nodupkey – How to Remove Duplicates with PROC SORT by Key

SAS nodupkey – How to Remove Duplicates with PROC SORT by Key

September 23, 2022 Leave a Comment

When using PROC SORT in SAS, you can use the ‘nodupkey’ option to remove observations with duplicate BY values. In other words, you can remove duplicates by key variables.

data example;
input a b;
datalines;
1 2
1 3
1 4
2 5
2 6
2 7
2 8
;
run;

proc sort data=example nodupkey;
    by a;
run;

/* example After PROC SORT */
    a    b
    1    2
    2    5

When working with data, the ability to remove duplicates from your data can be very valuable.

One such case when working with data in SAS is if you want to remove duplicate values based on certain columns in a dataset.

PROC SORT is most used to sort data in SAS, but you can also use PROC SORT to remove duplicates with different options.

When using PROC SORT in SAS, you can use the ‘nodupkey’ option to remove observations with duplicate BY values. In other words, you can remove duplicates by key variables.

If you use the ‘nodupkey’ option, typically you will keep the first observation and remove all other duplicates in the specified column. (This depends on certain global and PROC SORT options you can set)

Below is a simple example showing you how to use ‘nodupkey’ with PROC SORT in SAS.

data example;
input a b;
datalines;
1 2
1 3
1 4
2 5
2 6
2 7
2 8
;
run;

proc sort data=example nodupkey;
    by a;
run;

/* example After PROC SORT */
    a    b
    1    2
    2    5

The Difference Between nodupkey and nodup Options When Using PROC SORT in SAS

PROC SORT gives many different options for you to use which can change the behavior of what PROC SORT does.

Another useful option when using PROC SORT is ‘nodup’. ‘nodup’ removes duplicate observations and looks at the entire observation instead of just specified columns.

This is a difference between ‘nodup’ and ‘nodupkey’.

It is different as ‘nodupkey’ removes duplicates based on specific columns and ‘nodup’ doesn’t consider specified columns.

In the example above, if we used ‘nodup’, we would get back the entire dataset since there are no duplicate observations.

data example;
input a b;
datalines;
1 2
1 3
1 4
2 5
2 6
2 7
2 8
;
run;

proc sort data=example nodup;
    by a;
run;

/* example After PROC SORT */
    a    b
    1    2
    1    3
    1    4
    2    5
    2    6
    2    7
    2    8

Below is a slightly different dataset with some duplicates. Let’s see what happens when we use ‘nodup’ now.

data example;
input a b;
datalines;
1 2
1 2
1 2
2 6
2 6
2 6
2 8
;
run;

proc sort data=example nodup;
    by a;
run;

/* example After PROC SORT */
    a    b
    1    2
    2    6
    2    8

Hopefully this article has been useful for you to learn how to use the ‘nodupkey’ option when using PROC SORT in SAS to remove duplicate by key.

Other Articles You'll Also Like:

  • 1.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 2.  SAS Remove Formats from Dataset with PROC DATASETS
  • 3.  SAS month function – Get Month from Date Variable
  • 4.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 5.  SAS select when – Evaluating Character Values for Conditional Processing
  • 6.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 7.  Using SAS to Sum by Group with PROC MEANS
  • 8.  countw SAS – Count Number of Words in a String
  • 9.  nodup vs nodupkey PROC SORT Options in SAS
  • 10.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy