proc sql;
create table Members as
select ID, L_Name, Join_Date, Member_Type, Area, Member_Code,Partner_Code, Supvr_Code, Refund_Amt, Balance_Owed
from tbl_Membership
where Member_Type in ('Loss','Active','Contingent') and Member_Code ne '3' or
Member_Code eq '3' and Area = '24' or
Partner_Code eq '1' and Supvr_Code = '5' and
Balance_Owed >0;
quit;
In my output, I am getting some records with a zero. I am trying to determine when and where to enclose my or groups and "and' gropus in parenthesis. I do not deal with this many scenarios often so I want to get a good feel for general rules when dealing with an assortment of and vs or groups.
Parenthesis are used for specifying the order of how AND/OR are evaluated. Otherwise I think they're evaluated from left to right, which isn't necessarily what you want.
I'll add in the business logic in comments as well so it makes sense to me in the future and for updates.
WHERE
((Member_Type in ('Loss','Active','Contingent') and Member_Code ne '3') /*Member is active (ne 3) and delinquent*/
or (Member_Code eq '3' and Area = '24') /* OR Member is active and in Los Angeles*/
or (Partner_Code eq '1' and Supvr_Code = '5') ) /* OR Accounts where the partner is senior and supervisor is not*/
and Balance_Owed >0 /*Any of the above AND Accounts that have a balance outstanding*/
;
Here are some references on
Evaluating Logical Expressions
http://www.sascommunity.org/wiki/Evaluating_Logical_Expressions
This is the support.sas page on order of precedence of evaluation of logical operators
Defining Arithmetic and Logical Expressions
SAS(R) 9.3 Macro Language: Reference
Ron Fehd logically yours
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.