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
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;
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.
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
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 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.