Hi SAS Pros,
I am going to compare the difference between the responses of some variables and get the difference printed out.
There are a series variables titled fruit1 fruit2 fruit3 fruit4.
This is what I have:
Fruit1 | Fruit2 | Fruit3 | Fruit4 |
apple | pear | apple | apples |
pear | blueberry | pears | pears |
banana | strawberry | avocado | Banana |
avocado | grape | blueberry | avocado |
grape | apple | strawberry | |
grape | |||
date |
I want to compare fruit2 fruit3 and fruit4 against fruit1 and print out anyone that fruit1 doesn't have but emerges in fruit2, fruit3, and fruit4. There is no need to compare between fruit2 and fruit3 and etc, only compared with fruit1. Also, the comparison is case sensitive, which means Banana is different from banana.
Want:
differences in fruit2 | differences in fruit3 | differences in fruit4 |
blueberry | pears | apples |
strawberry | blueberry | pears |
strawberry | Banana | |
date |
Thank you in advance for any help!!!
Best regards,
C
data have;
infile cards truncover;
input (Fruit1 Fruit2 Fruit3 Fruit4) (:$20.);
cards;
apple pear apple apples
pear blueberry pears pears
banana strawberry avocado Banana
avocado grape blueberry avocado
grape apple strawberry
. . grape . .
. . date . .
;
data want;
if _n_=1 then do;
dcl hash H (dataset:'have') ;
h.definekey ("fruit1") ;
h.definedone () ;
end;
set have;
array ff fruit2-fruit4;
array f $20 fruit_diff2-fruit_diff4;
do over f;
if h.check(key:ff) ne 0 then f=ff;
end;
run;
data have;
infile cards truncover;
input (Fruit1 Fruit2 Fruit3 Fruit4) (:$20.);
cards;
apple pear apple apples
pear blueberry pears pears
banana strawberry avocado Banana
avocado grape blueberry avocado
grape apple strawberry
. . grape . .
. . date . .
;
data want;
if _n_=1 then do;
dcl hash H (dataset:'have') ;
h.definekey ("fruit1") ;
h.definedone () ;
end;
set have;
array ff fruit2-fruit4;
array f $20 fruit_diff2-fruit_diff4;
do over f;
if h.check(key:ff) ne 0 then f=ff;
end;
run;
Hi,
Thank you so much for the code. It works great!
Best regards,
C
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.