Solved
Contributor
Posts: 70

# AND OR NOT expressions logical way of writing correction please?

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

Accepted Solutions
Solution
‎07-01-2015 03:01 PM
Super User
Posts: 7,934

## Re: AND OR NOT expressions logical way of writing correction please?

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;

All Replies
Super User
Posts: 23,296

## Re: AND OR NOT expressions logical way of writing correction please?

You need to define CORRECT.

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

Contributor
Posts: 70

## Re: AND OR NOT expressions logical way of writing correction please?

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

Super User
Posts: 23,296

## Re: AND OR NOT expressions logical way of writing correction please?

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

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

Solution
‎07-01-2015 03:01 PM
Super User
Posts: 7,934

## Re: AND OR NOT expressions logical way of writing correction please?

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;

Contributor
Posts: 70

## Re: AND OR NOT expressions logical way of writing correction please?

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

PROC Star
Posts: 1,288

## Re: AND OR NOT expressions logical way of writing correction please?

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

🔒 This topic is solved and locked.