Table of Contents
The recommended method for removing duplicate values in SAS is by using the PROC SORT procedure with the NODUPKEY option. This option ensures that only unique values are retained in the sorted dataset, eliminating any duplicate values. This method is efficient and convenient as it does not require additional coding or complex data manipulation techniques. By using this approach, data analysts can easily identify and remove duplicate values, ensuring the accuracy and reliability of their analysis results. Additionally, the NODUPKEY option can be applied to multiple variables, making it a versatile solution for handling duplicate values in SAS.
SAS: Use PROC SORT with NODUPKEY
You can use PROC SORT in SAS with NODUPKEY to order the observations in a dataset by one or more variables and remove any duplicates.
The following example shows how to use this procedure with the following dataset in SAS:
/*create dataset*/
data original_data;
input team $ points rebounds;
datalines;
A 12 8
A 12 8
A 12 8
A 23 9
A 20 12
A 14 7
A 14 7
B 20 2
B 20 5
B 29 4
B 14 7
B 20 2
B 20 2
B 20 5
;
run;
/*view dataset*/
proc printdata=original_data;

Example: Using PROC SORT with NODUPKEY in SAS
Suppose we simply use proc sort to sort the observations in the dataset in ascending order (smallest to largest) based on the value in the points column:
/*sort by points ascending*/
proc sortdata=original_data out=data2;
by points;
run;
/*view sorted dataset*/
proc printdata=data2;
Notice that the observations are sorted in ascending order based on the value in the points column.
However, there are several observations that are duplicates.
To sort the observations based on the values in the points column and remove all duplicates, we can add nodupkey after the proc sort statement:
/*sort by points ascending and remove duplicates*/
proc sortdata=original_data out=data3 nodupkey;
by points;
run;
/*view sorted dataset*/
proc printdata=data3;
The observations are now sorted in ascending order based on the value in the points column and all duplicate observations have been removed.
Note that we can also add the argument descending to instead sort the observations based on the value in the points column in descending order and remove all duplicates:
/*sort by points descending and remove duplicates*/
proc sortdata=original_data out=data4 nodupkey;
bydescending points;
run;
/*view sorted dataset*/
proc printdata=data4;
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). “What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-recommended-method-for-removing-duplicate-values-in-sas-using-proc-sort-with-the-nodupkey-option/
stats writer. "“What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/what-is-the-recommended-method-for-removing-duplicate-values-in-sas-using-proc-sort-with-the-nodupkey-option/.
stats writer. "“What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-recommended-method-for-removing-duplicate-values-in-sas-using-proc-sort-with-the-nodupkey-option/.
stats writer (2024) '“What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-recommended-method-for-removing-duplicate-values-in-sas-using-proc-sort-with-the-nodupkey-option/.
[1] stats writer, "“What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. “What is the recommended method for removing duplicate values in SAS using PROC SORT with the NODUPKEY option?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
