BookmarkSubscribeRSS Feed
omega1983
Calcite | Level 5

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.

2 REPLIES 2
Reeza
Super User

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*/

;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

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!

What is Bayesian Analysis?

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.

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
  • 2 replies
  • 648 views
  • 3 likes
  • 3 in conversation