Desktop productivity for business analysts and programmers

IF STATEMENT comparing one to many

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

IF STATEMENT comparing one to many

Hi,

I have one variable I want to compare to 21 others.  I only want IF the variable CONTROL NOT EQUALS variables CONTROL1 to CONTROL21.  Anyone has a better, simpler way of doing it than this?

IF CONTROL NE CONTROL1 AND CONTROL NE CONTROL2 AND CONTROL NE CONTROL3 AND CONTROL NE CONTROL 4.................. AND  CONTROL  NE CONTROL 21;

Thank you for the help;

Ismael


Accepted Solutions
Solution
‎10-30-2014 02:22 PM
Valued Guide
Posts: 763

Re: IF STATEMENT comparing one to many

Nice ... would just change > 0 to eq 0 since looking for observations where CONTROL is NOT EQUAL to any of CONTOL1-CONTROL5, yes/no?

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: IF STATEMENT comparing one to many

You don't give many details.  For instance is it a character to character check, then maybe:

     where catx(',',of control1-control21) not contains xyz;

I.e. if the string in variable xyz does not appear in a concatenation of all the others.

You could also do arrays:

array control{21};

do I=1 to 21;

     if xyz=control{I} then found=1;

end;

if found=0 then ...

You could generate the code from a data _null_ using call execute, you could create a macro etc.

Valued Guide
Posts: 763

Re: IF STATEMENT comparing one to many

Hi.  Re ...

"For instance is it a character to character check, then maybe:      where catx(',',of control1-control21) not contains xyz;"

The CAT functions can be used with both CHARACTER and NUMERIC variables, for example ...

data test;

input control control1-control5;

datalines;

1 1 2 3 4 5

2 9 9 9 9 9

5 9 9 9 9 5

9 1 2 9 9 9

8 1 1 1 1 1

;

data new;

set test;

if ^find(cat(of control1-control5), cat(control));

run;

works with no annoying messages in the LOG.

ps ... Searching for Variable Values with CAT Functions: An Alternative to Arrays and Loops

http://www.nesug.org/Proceedings/nesug09/ff/ff04.pdf

Valued Guide
Posts: 763

Re: IF STATEMENT comparing one to many

Hi ... try this ...

data test;

input control control1-control5;

datalines;

1 1 2 3 4 5

2 9 9 9 9 9

5 9 9 9 9 5

9 1 2 9 9 9

8 1 1 1 1 1

;

data new;

set test;

array x(5) control1-control5;

if control not in x;

run;

ps  Learned about this use of an array from Ksharp a while back.

Grand Advisor
Posts: 9,576

Re: IF STATEMENT comparing one to many

Mike,

Actually , I learned it from data _null_; Smiley Happy

Respected Advisor
Posts: 4,606

Re: IF STATEMENT comparing one to many

How about

if whichn(control, of control1-control21) > 0; /* if NUMERIC */

if whichc(control, of control1-control21) > 0; /* if CHARACTER */

PG

PG
Solution
‎10-30-2014 02:22 PM
Valued Guide
Posts: 763

Re: IF STATEMENT comparing one to many

Nice ... would just change > 0 to eq 0 since looking for observations where CONTROL is NOT EQUAL to any of CONTOL1-CONTROL5, yes/no?

Respected Advisor
Posts: 4,606

Re: IF STATEMENT comparing one to many

Right! Smiley Happy

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 450 views
  • 5 likes
  • 5 in conversation