Help using Base SAS procedures

Remove duplicate values across rows

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Remove duplicate values across rows

Hello, I need to remove duplicates in a SAS dataset but don't know how to go about it. The point of the dataset is to have all CBSAs in the nation listed in the first column. Additional columns contain every CBSA which borders the first one. The problem is that the original CBSA is also always listed as a bordering one, and I need to remove it. The other problem is that the duplicate doesn't always appear in the same column. For example, I have:

 

CBSA          CBSA_BORD1          CBSA_BORD2          CBSA_BORD3

10140          10140                        16500                         36500

10180          10180                        15220                         45020   

28300          14180                        27860                         28300

28500          23240                        28500                         41700

 

The bold values are the duplicates, i.e. the ones I want to remove from the dataset. The problem is that I don't know which column the duplicate will necessarily be in since it changes row to row. Is there a way to search for duplicate values across rows and remove them? What I want the final dataset to look like is:

 

CBSA          CBSA_BORD1          CBSA_BORD2         

10140          16500                        36500

10180          15220                        45020   

28300          14180                        27860                         

28500          23240                        41700

 

Thank you!


Accepted Solutions
Solution
‎06-07-2016 12:11 PM
Super User
Super User
Posts: 7,942

Re: Remove duplicate values across rows

Hi,

 

 

It is generally better to post test data in the form of a datastep.  You can use arrays on this:

data want;
  cbsa=10140; cbsa_bord1=10140; cbsa_bord2=16500;  cbsa_bord3=36500;
  array cbsa_bord{3};
  do i=1 to 3;
    if cbsa_bord{i}=cbsa then cbsa_bord{i}=.;
  end;
run;

View solution in original post


All Replies
Solution
‎06-07-2016 12:11 PM
Super User
Super User
Posts: 7,942

Re: Remove duplicate values across rows

Hi,

 

 

It is generally better to post test data in the form of a datastep.  You can use arrays on this:

data want;
  cbsa=10140; cbsa_bord1=10140; cbsa_bord2=16500;  cbsa_bord3=36500;
  array cbsa_bord{3};
  do i=1 to 3;
    if cbsa_bord{i}=cbsa then cbsa_bord{i}=.;
  end;
run;
Contributor
Posts: 20

Re: Remove duplicate values across rows

Perfect, don't know why I didn't think of that. Thanks so much!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 373 views
  • 0 likes
  • 2 in conversation