---
title: "The Easy Way Removing Duplicates in SAS"
description: "The example macro help you to remove duplicates in SAS macro"
url: https://srinimf.com/2018/03/11/sas-eliminate-duplicates/
date: 2018-03-11
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2018/03/sas-macro.jpg?fit=800%2C533&ssl=1
categories: ["SAS"]
tags: ["Eliminate duplicate in SAS", "SAS nodupkey", "SAS tips and tricks"]
type: post
lang: en
---

# The Easy Way Removing Duplicates in SAS

Here’s a sample macro that shows removing duplicates in SAS. This question asked in [Synchrony](https://www.synchronyfinancial.com) interview.

Related[Explained SAS Macro Here’s Tutorial](https://srinimf.com/2012/05/19/mainframe-with-sas/)

## **Here’s [input dataset](https://srinimf.com/2015/10/16/jcl-to-sort-few-fields-in-an-input-dataset/) having duplicate rows**

```
beluga  whale 15
whale   shark 40
basking shark 30
gray    whale 50
mako   shark 12
sperm  whale 60
dwarf  shark .5
whale  shark 40
humpback   . 50
blue   whale 100
killer whale 30
```

Related[SAS: 10 Quiz Questions on Files](https://srinimf.com/2018/03/24/sas-files-real-use-awesome-quiz/)

## **[SAS Macro](https://srinimf.com/2016/03/10/10-awesome-step-to-writing-sas-programin-zos/)**

```
DATA marine;
 INFILE 'c:\MyRawData\Sealife.dat';
 INPUT Name $ Family $ Length;
* Sort the data;
PROC SORT DATA = marine OUT = seasort NODUPKEY;
 BY Family DESCENDING Length;
PROC PRINT DATA = seasort;
 TITLE 'Whales and Sharks 1';
RUN;
```

The DATA step reads the raw data from a file called Sealife.dat and creates a SAS data set named MARINE.

Then PROC SORT rearranges the observations by family in ascending order, and by
length in descending order. While the OUT= option writes the sorted data into a new data set named SEASORT.

> SpotlightThe NODUPKEY option of PROC SORT eliminates duplicates if any

The output from PROC PRINT looks like this:

```
Whales and Sharks 1
 Obs    Name           Family    Length
 1      humpback                 50.0
 2      whale          shark     40.0
 3      basking        shark    30.0
 4      mako           shark    12.0
 5      dwarf          shark    0.5
 6      blue           whale    100.0
 7      sperm          whale   60.0
 8      gray           whale   50.0
 9      killer         whale   30.0
 10     beluga         whale   15.0
```

Related

- [SAS: 10 Quiz Questions on Files](https://srinimf.com/2018/03/24/sas-files-real-use-awesome-quiz/)DateMarch 24, 2018
- [How to Start Your First SAS Macro](https://srinimf.com/2014/06/09/sas-mainframe-program/)DateJune 9, 2014
- [7 Must Know SAS Language Terms](https://srinimf.com/2018/05/15/terms-in-sas-macro-processing/)DateMay 15, 2018

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2018%2F03%2F11%2Fsas-eliminate-duplicates%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2018%2F03%2Fsas-macro.jpg%3Ffit%3D800%252C533%26ssl%3D1&description=The%20Easy%20Way%20Removing%20Duplicates%20in%20SAS)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2018/03/11/sas-eliminate-duplicates/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2018/03/11/sas-eliminate-duplicates/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2018/03/11/sas-eliminate-duplicates/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2018/03/11/sas-eliminate-duplicates/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20The%20Easy%20Way%20Removing%20Duplicates%20in%20SAS&body=https%3A%2F%2Fsrinimf.com%2F2018%2F03%2F11%2Fsas-eliminate-duplicates%2F&share=email)
-

### Like this:

Like Loading…