BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

data a1 ;
var1='abc' ;
var2='def' ;
var3='ghi' ;
var4='abc' ;
var5='xyz' ;
var6='def' ;
run;

I have above dataset. how to remove duplicate values column wise in arrays.

output come should come like see below example

var1=abc  var2='def'   var3='ghi'  var4=' '   var5='xyz'  var6=' '

var4 and var6 we have to assign missing values because there values already in var1 and var2.

I have tried below code. Kindly help me out.

code:

 

data a1 ;
var1='abc' ;
var2='def' ;
var3='ghi' ;
var4='abc' ;
var5='xyz' ;
var6='def' ;
run;

data a2 ;
set a1 ;
array apple(6) $ var1-var6 ;
do i=1 to 5;
do j=2 to 6 ;
if apple(i) ne ' ' and apple(j) ne ' ' then do ;
if (apple(i) ne ' ' and apple(j) ne ' ') and apple(i)=apple(j) then apple(i)=' ' ;
end;
end;
end;
run;

6 REPLIES 6
PaigeMiller
Diamond | Level 26
data a2 ;
    set a1 ;
    array apple(6) $ var1-var6 ;
    do i=1 to 5;
        do j=i+1 to 6 ;
            if apple(i) ne ' ' and apple(j) ne ' ' then do ;
                if apple(i)=apple(j) then apple(i)=' ' ;
            end;
        end;
    end;
run;

Please, from now on, post your code in a code box, as I have done, by clicking on the "running man" icon and then pasting your code into the box.

 

Also, do yourself a favor and indent your code as shown above. 

--
Paige Miller
andreas_lds
Jade | Level 19

Depending on the number of observation i would tend to transpose the data and use proc sort to remove the duplicates.

proc transpose data=a1 out=transposed;
   var var:;
run;

proc sort data=transposed out=nodups nodupkey;
   by col1;
run;
thanikondharish
Fluorite | Level 6
Sorry , We have to do in arrays only
PaigeMiller
Diamond | Level 26

@thanikondharish wrote:
Sorry , We have to do in arrays only

Why?

--
Paige Miller
PaigeMiller
Diamond | Level 26

For anyone who doesn't have the restriction of doing it in arrays (which I think would be almost everyone), I think @andreas_lds has the correct (and easiest) answer.

--
Paige Miller
Ksharp
Super User
data a1 ;
var1='abc' ;
var2='def' ;
var3='ghi' ;
var4='abc' ;
var5='xyz' ;
var6='def' ;
run;

data a2 ;
set a1 ;
array apple(6) $ var1-var6 ;
array temp(6) $ temp1-temp6 ;
do i=1 to dim(apple);
  if apple{i} in temp then call missing(apple{i});
   else temp{i}=apple{i};
end;
drop i temp:;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2882 views
  • 2 likes
  • 4 in conversation