Sample 44079: Removing duplicate values on an observation.
Using arrays, this sample shows how remove duplicate values from an observation. It also shows how to shift the non-missing values to the left, if that is a desired result.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This code uses arrays and IF logic to remove the duplicate values from an observation. The second part of the code shows how to shift all non-missing values to the left.
/*sample data*/
data a;
input Patient $ drug1 $ drug2 $ drug3 $ drug4 $ drug5 $;
datalines;
1 a b a c c
2 c d c . .
3 a b a f c
;
run;
proc print;
title 'data before duplicates are removed';
run;
data b;
set a;
retain hold;
/*ARRAY to group the DRUGn variables together*/
array drugs(5) drug1-drug5;
do i=1 to 5;
do j=1 to 5;
/*IF logic to determine if the values are the same and if so, set
the repeat value to missing*/
if i ne j and drugs(j)=drugs(i) then drugs(j)=' ';
end;
end;
/*this section of the code checks to see if a value is missing and if so,
shifts the non-missing values to the left*/
/*if the values are not to be shifted over, then leave this section of the
out*/
do k=1 to 5;
if drugs[k] = '' then do;
do l= k+1 to 5;
if drugs[l] ne '' then do;
drugs[k] = drugs[l];
drugs[l] = '';
leave;
end;
end;
end;
end;
drop i j k l;
run;
proc print;
title 'data after duplicates have been removed';
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
data before duplicates are removed
Obs Patient drug1 drug2 drug3 drug4 drug5
1 1 a b a c c
2 2 c d c
3 3 a b a f c
data after duplicates have been removed
Obs Patient drug1 drug2 drug3 drug4 drug5
1 1 a b c
2 2 c d
3 3 a b f c
This sample shows how remove duplicate values from an observation and then, if desired, shift the non-missing values to the left.
| Date Modified: | 2011-08-24 15:37:03 |
| Date Created: | 2011-08-23 13:24:42 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | | |
| Z64 | | |
| OpenVMS VAX | | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
| Microsoft Windows XP 64-bit Edition | | |
| Microsoft® Windows® for x64 | | |
| OS/2 | | |
| Microsoft Windows 95/98 | | |
| Microsoft Windows 2000 Advanced Server | | |
| Microsoft Windows 2000 Datacenter Server | | |
| Microsoft Windows 2000 Server | | |
| Microsoft Windows 2000 Professional | | |
| Microsoft Windows NT Workstation | | |
| Microsoft Windows Server 2003 Datacenter Edition | | |
| Microsoft Windows Server 2003 Enterprise Edition | | |
| Microsoft Windows Server 2003 Standard Edition | | |
| Microsoft Windows Server 2003 for x64 | | |
| Microsoft Windows Server 2008 | | |
| Microsoft Windows Server 2008 for x64 | | |
| Microsoft Windows XP Professional | | |
| Windows 7 Enterprise 32 bit | | |
| Windows 7 Enterprise x64 | | |
| Windows 7 Home Premium 32 bit | | |
| Windows 7 Home Premium x64 | | |
| Windows 7 Professional 32 bit | | |
| Windows 7 Professional x64 | | |
| Windows 7 Ultimate 32 bit | | |
| Windows 7 Ultimate x64 | | |
| Windows Millennium Edition (Me) | | |
| Windows Vista | | |
| Windows Vista for x64 | | |
| 64-bit Enabled AIX | | |
| 64-bit Enabled HP-UX | | |
| 64-bit Enabled Solaris | | |
| ABI+ for Intel Architecture | | |
| AIX | | |
| HP-UX | | |
| HP-UX IPF | | |
| IRIX | | |
| Linux | | |
| Linux for x64 | | |
| Linux on Itanium | | |
| OpenVMS Alpha | | |
| OpenVMS on HP Integrity | | |
| Solaris | | |
| Solaris for x64 | | |
| Tru64 UNIX | | |