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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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