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

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

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

MikeZdeb
Rhodochrosite | Level 12

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

MikeZdeb
Rhodochrosite | Level 12

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.

Ksharp
Super User

Mike,

Actually , I learned it from data _null_; Smiley Happy

PGStats
Opal | Level 21

How about

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

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

PG

PG
MikeZdeb
Rhodochrosite | Level 12

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

PGStats
Opal | Level 21

Right! Smiley Happy

PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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