BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CynthiaWei
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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;
CynthiaWei
Obsidian | Level 7

Hi,

 

Thank you so much for the code. It works great!

 

Best regards,

 

C

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 616 views
  • 0 likes
  • 2 in conversation