DATA Step, Macro, Functions and more

Compare multiple character variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Compare multiple character variables

Hello,

 

I would like to compare multiple character variable values let say c1,c2,c3 and c4

the result should be 1 if all the values are not equal.

The result should'nt be 1 if one of the variable is empty.

 

Thanks for the help!


Accepted Solutions
Solution
‎03-02-2018 02:47 PM
Trusted Advisor
Posts: 1,387

Re: Compare multiple character variables

OK, so a blank is not meant to establish an inequality.  Then:

 

data want (drop=i word);

  set have;
  array c{4} $ c1-c4;

  word=scan(catx(' ',of c{*}),1);

  result='same';
  do I=1 to dim(c) while (result='same');
    if NOT(c{i}=word or c{i}='') then result='diff';
  end;

run;

This strategy here is the same as my previous submission.  Default result to 'same' until proven otherwise.

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,387

Re: Compare multiple character variables

Show us what you have tried so far.  Then we can help you help yourself.

Occasional Contributor
Posts: 12

Re: Compare multiple character variables

data want(drop=count i);
 set have;
 array _v{*} c1 c2 c3 c4;
 count=0;
 do i=1 to dim(_v);
  count+(_v{1}=_v{i});
 end;
flag=ifc(count=dim(_v),'Same','Diff');
run;

This is what i tried so far.

Trusted Advisor
Posts: 1,387

Re: Compare multiple character variables

Here's an approach that is coded to avoid the loop if there are missing values, and to stop stepping through the loop once a non-equality is encountered:

 

data want (drop=I);

  set have;

  array c{*} $ c1 c2 c3 c4;

  if cmiss(of c{*})=0 then result='same';
  else result='diff';

  do I=2 to dim(c) while (result='same');
    if c{i}^=c{i-1} then result='diff';
  end;

run;
Super User
Posts: 23,937

Re: Compare multiple character variables

So if C1=C2 then the output should be? 

Do you only have 4 or does this need to be expanded?

Untested but perhaps something like this. 


Use CMISS() to check for missing and then loop through the data and check if each value is unique. The last value doesn't need to be checked.

 

 

data want;

set have;

 

array _c(4) $ c1-c4;

if cmiss(of c1-c4) = 0 then do i=1 to dim(_c)-1;

if whichc(_c(i) of _c(*)) >1 then do;

flag=1;

leave;

end;

end;

 

if flag ne 1 then results=1;

else flag=0;

 

run;

 

 

Occasional Contributor
Posts: 12

Re: Compare multiple character variables

c1--c2--c3--c4--result

a--a--a--a--same

a--a--{empty}--a--same

a--b--a--a--not same

a--b--{empty}--a--not same

 

This is what I want.

Thanks

Solution
‎03-02-2018 02:47 PM
Trusted Advisor
Posts: 1,387

Re: Compare multiple character variables

OK, so a blank is not meant to establish an inequality.  Then:

 

data want (drop=i word);

  set have;
  array c{4} $ c1-c4;

  word=scan(catx(' ',of c{*}),1);

  result='same';
  do I=1 to dim(c) while (result='same');
    if NOT(c{i}=word or c{i}='') then result='diff';
  end;

run;

This strategy here is the same as my previous submission.  Default result to 'same' until proven otherwise.

 

Occasional Contributor
Posts: 12

Re: Compare multiple character variables

[ Edited ]

awesome! thank you.
could you please explain this:

if NOT(c{i}=word or c{i}='') then result='diff';


thanks

Trusted Advisor
Posts: 1,387

Re: Compare multiple character variables

The logical expression  NOT(a or b) is the same as   NOT(a) and NOT(b), so

 

if NOT(c{i}=word or c{i}='') then result='diff';

is the same as

 

if c{i}^=word and c{i}^=' ' then result='diff';

Since WORD is the first non-blank among c{1}-c{4}, this IF expression tests for whether there is inequality among non-blank values in your array.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 236 views
  • 0 likes
  • 3 in conversation