Multiple conditions with 'and' 'or' operators

Accepted Solution Solved
Reply
Contributor kbk
Contributor
Posts: 29
Accepted Solution

Multiple conditions with 'and' 'or' operators

This is relatively minor but I was unable to gain an explanation via a Google search. In the example below, I am trying to drop all data rows that are NULL for both Flag1 and Flag2. In my mind, you would accomplish this via a subsetting IF statement where you use two conditions, separated by an AND. I. e., you want to drop values that are NULL for Flag1 and Flag2. However, the correct output is obtained with an OR statement. This baffles me because I would expect an OR statement to drop all rows that have a NULL in either Flag1 or Flag2!

Can someone please set me right on this?


---------------------


data have;

input MI $ Flag1 Flag2;

datalines;                    

1 1 1

1 . .

2 0 .

2 1 .

2 . .

3 0 1

4 1 .

5 . .

6 0 0

6 1 .

6 . .

;

run;

proc freq data=have;

     tables flag1*flag2 /missing;

run;

*this drops rows where either flag is NULL;

data want1;

     set have;

     if flag1 ~= . and flag2 ~= .;

run;

*this drops rows where both flags are NULL;

data want2;

     set have;

     if flag1 ~= . OR flag2 ~= .;

run;


Accepted Solutions
Solution
‎03-10-2014 03:29 PM
Super User
Posts: 11,343

Re: Multiple conditions with 'and' 'or' operators

You might want to look up "logic tables". Your issue is because of the NOT inclusion.

Your statement

if flag1 ~= . and flag2 ~= .;

say to keep the record when both are not missing. If only one is missing then both actually are not missing and the record is not retained.

View solution in original post


All Replies
Solution
‎03-10-2014 03:29 PM
Super User
Posts: 11,343

Re: Multiple conditions with 'and' 'or' operators

You might want to look up "logic tables". Your issue is because of the NOT inclusion.

Your statement

if flag1 ~= . and flag2 ~= .;

say to keep the record when both are not missing. If only one is missing then both actually are not missing and the record is not retained.

Super User
Posts: 19,877

Re: Multiple conditions with 'and' 'or' operators

You aren't specifying which rows to DROP, you're specifying which rows to KEEP.

If you want to delete rows then add then delete, or change your condition.

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 183 views
  • 3 likes
  • 3 in conversation