BookmarkSubscribeRSS Feed
Sir_Lancelot
Fluorite | Level 6

I am trying to compare two fields and output the difference. Please see below example:

 

field_1field_2results
003,004,006003,004006
002,003,005002,005003
003,004003,004,006006
002,005002,003,005003

 

Thanks in advance

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Will it always be like that, i.e. field_2 as a subet of field_1?  If so:

results=strip(tranwrd(field_1,field_2,""));

If not then you will need to loop through then:

data want;
  set have;
  do i=1 to countw(field_2,",");
    if findw(field_1,scan(field_2,i,","),",") then results=scan(field_2,i,",");
  end;
run;

Note that this only assumes one difference, other wise you will need:

    if findw(field_1,scan(field_2,i,","),",") then results=catx(",",results,scan(field_2,i,","));
Astounding
PROC Star

Do I need a cup of coffee, or should the FINDW result be compared to 0?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would have tea myself.  But yes, I was going with the reverse mistakenly.  You can either compare with zero or not() it:

data want;
  set have;
  do i=1 to countw(field_2,",");
    if not(findw(field_1,scan(field_2,i,","),",")) then results=scan(field_2,i,",");
  end;
run;
Ksharp
Super User
data have;
input field_1 : $20.	field_2	:$20.;
a=translate(field_1,'|',',');
b=translate(field_2,'|',',');
x1=prxchange(cats('s/',b,'//'),-1,translate(field_1,' ',','));
x2=prxchange(cats('s/',a,'//'),-1,translate(field_2,' ',','));

want=catx(' ',x1,x2);
drop a b x1 x2;
cards;
003,004,006    003,004
002,003,005     002,005
003,004        003,004,006
002,005      002,003,005
;
run;

proc print noobs;run;
ballardw
Super User

@Sir_Lancelot wrote:

I am trying to compare two fields and output the difference. Please see below example:

 

field_1 field_2 results
003,004,006 003,004 006
002,003,005 002,005 003
003,004 003,004,006 006
002,005 002,003,005 003

 

Thanks in advance


And perhaps yet another example why having multiple values in a single field such as "003, 004, 006" is a poor idea in general.

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
  • 5 replies
  • 1284 views
  • 3 likes
  • 5 in conversation