BookmarkSubscribeRSS Feed
Haydo
Obsidian | Level 7

Hi.

 

I have the following code and clearly I am not doing something right.

The value of Rule3 is return the value of "No", therefore I want the value of "RegOff" to be "No", but if the value of Rule3 is "Yes", then apply the and plus or statements.

Sorry if this is an easy one, but perhaps I've been looking at it too long and my head is going in circles.

 

	if 'INT Type'n =1  and RGO=1 or IDF=1 then Rule3="Yes";
	else Rule3="No";
    if Rule3="Yes" and ADM >0 or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0 Then RegOff="Yes";
	else RegOff="No";

Cheers

Haydn

4 REPLIES 4
Kurt_Bremser
Super User

IF is a statement, OR is an operator.

 

In logic, AND takes precedence over OR. Therefore your conditions are equivalent to this:

if ('INT Type'n =1  and RGO=1) or IDF=1 then Rule3="Yes";
if (Rule3="Yes" and ADM >0) or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0 then RegOff="Yes";
Haydo
Obsidian | Level 7
Hi Kurt,

Thanks for your quick response. I copied your code and Rule 3 is blank (as desired), however RegOff is returning the value of "Yes". But given Rule 3 does not have the value of "Yes", it should not.
Kurt_Bremser
Super User

My examples are only there to illustrate what your conditions as posted really do.

It is now up to you to set the parentheses in a way so that the code reflects what you want to achieve.

If all the ORs in a compound condition should only play a role if the first condition is met, you need to do

if a and (x or y or z)

Review both of your IF statements in light of this.

 

 

ballardw
Super User

Hint: if you have many numeric variables and you want to find if any (one or more) of them have a value greater than 0 you can reduce code like this (Assuming the intent is to use all of them to get one logical result)

 ADM >0 or ALC >0 or CAR>0 or CDM >0 or MPR >0 or NCP >0 or PTR >0 or DMA >0 or DMC >0 or IDD >0

shorter:

max( ADM, ALC, CAR, CDM, MPR, NCP, PTR, DMA, DMC, IDD)>0

You would have to provide values of the variables, the incorrect result and the expected result for the values to determine the actual issues in your comparison approach.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 687 views
  • 2 likes
  • 3 in conversation