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

Hi, I would like to know what is the correct way to write or in other words use AND OR NOT operators in SAS?

Let's us suppose we have 3 variables with names A B C in a dataset

/*which way of syntax is correct*?*/

if missing(a) or missing(b) and missing(c) then.....  is this correct? or

if (missing(a) or missing(b)) and missing(c) then......is is correct, meaning with the parenthesis?

another condition:

if missing(a) and missing(b) and missing(c) then......is this correct? or

if (missing(a) and missing(b)) and missing(c) then...is is correct, meaning with the parenthesis?

Please clarify the need for parenthesis. Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Try it and see.

%let tests=

a and b or c

|(a and b) or c

|a and (b or c)

|a or b and c

|(a or b) and c

|a or (b and c)

;

%macro xx ;

data xx;

do a=0,1 ;

  do b=0,1;

   do c=0,1;

     put (a b c) (=);

%do i=1 %to %sysfunc(countw(&tests,|));

     testn=&i;

     test= "%scan(&tests,&i,|)";

     val= %scan(&tests,&i,|);

     put (testn test val) (=);

     output;

%end;

   end; end; end;

run;

%mend xx;

%xx;

View solution in original post

6 REPLIES 6
Reeza
Super User

You need to define CORRECT. 

All will work but may generate different results so it depends on what you're looking for.

Andygray
Quartz | Level 8

I am just trying to learn and understand by asking how the condition is checked with and without parentheses and how the results differ. Thanks

Reeza
Super User

There is also documentation on the Order of Operations in Compound Expressions:

SAS(R) 9.2 Language Reference: Concepts, Second Edition

Tom
Super User Tom
Super User

Try it and see.

%let tests=

a and b or c

|(a and b) or c

|a and (b or c)

|a or b and c

|(a or b) and c

|a or (b and c)

;

%macro xx ;

data xx;

do a=0,1 ;

  do b=0,1;

   do c=0,1;

     put (a b c) (=);

%do i=1 %to %sysfunc(countw(&tests,|));

     testn=&i;

     test= "%scan(&tests,&i,|)";

     val= %scan(&tests,&i,|);

     put (testn test val) (=);

     output;

%end;

   end; end; end;

run;

%mend xx;

%xx;

Andygray
Quartz | Level 8

Beautiful. That clears all my doubts in my brain. Thanks Tom!

TomKari
Onyx | Level 15

Personally, I make it a habit to use parentheses to clarify the order that I want for the evaluation, even if it matches the order without parentheses. This is for two reasons; first, different languages use different rules, and it's easy to make a mistake, and second, when someone else is looking at the code, they may not know the correct rules, and may mistake what the code is doing.

On the other hand, I don't see any downside to using extra parentheses to make is explicitly clear.

Tom

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!

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
  • 6 replies
  • 1546 views
  • 1 like
  • 4 in conversation