BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

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
Calcite | Level 5
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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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