BookmarkSubscribeRSS Feed
Sas97
Calcite | Level 5

How to include multiple "AND" and "OR" in an IF statement?

 

if

'5.1b'n = 0 or '5.1b'n = 1

and '5.2a'n = 0 and '5.3a'n = 0 and '5.4a'n = 0 and
'5.5a'n = 0 and '5.6a'n = 0 and '5.7a'n = 0 and '5.8a'n = 0 and '5.9a'n = 0

then

variable = result

3 REPLIES 3
PaigeMiller
Diamond | Level 26

What is wrong with the code you show (other than it is missing a semi-colon)?


Do you want something like this, where there are parentheses added so that the OR conditions are executed together?

 

if

('5.1b'n = 0 or '5.1b'n = 1)

and '5.2a'n = 0 and '5.3a'n = 0 and '5.4a'n = 0 and
'5.5a'n = 0 and '5.6a'n = 0 and '5.7a'n = 0 and '5.8a'n = 0 and '5.9a'n = 0

then

variable = result;

 

--
Paige Miller
Kurt_Bremser
Super User

AND is evaluated before OR, so your condition is equivalent to this:

if
  '5.1b'n = 0 or (
    '5.1b'n = 1 and '5.2a'n = 0 and '5.3a'n = 0 and '5.4a'n = 0 and
    '5.5a'n = 0 and '5.6a'n = 0 and '5.7a'n = 0 and '5.8a'n = 0 and '5.9a'n = 0
  )
then variable = result;

Why do you make your life difficult by having non-standard names which force you to use name literals?

Use underlines instead of the dots.

FreelanceReinh
Jade | Level 19

Hello @Sas97 and welcome to the SAS Support Communities!

 

By using the IN operator and the CAT function (applied to a name range list) you can possibly avoid multiple "AND" and "OR" in your particular IF condition:

if '5.1b'n in (0 1) & cat(of '5.2a'n--'5.9a'n) = '00000000' then variable = result;

This assumes that

  1. You want the condition to be evaluated the way Paige Miller has suggested.
  2. Variable '5.2a'n is the first and '5.9a'n is the last of eight consecutive variables including also '5.3a'n, ..., '5.8a'n in the program data vector of your DATA step.Typically, these eight variables would occur in this order in PROC CONTENTS output (using the VARNUM option) of a dataset that is processed in the DATA step.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 312 views
  • 4 likes
  • 4 in conversation