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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.