DATA Step, Macro, Functions and more

%IF Statement and macro resolution

Accepted Solution Solved
Reply
Occasional Learner
Posts: 1
Accepted Solution

%IF Statement and macro resolution

I am using a %if statement inside a macro to execute code when &var does NOT match eye or hand. But the code inside the %if is still submitting when it gets to var = eye. How can I write this so that body = &var + 1; does NOT happen when var equals hand or eye?

 

 

 

data x;

set y;

%macro look (var);

%if &var ne hand or &var ne eye %then %do;

body = &var + 1;

%end;

%mend look;

%look (hand);

%look (eye);

%look (arm);

run;

 

 


Accepted Solutions
Solution
‎02-28-2017 04:49 PM
Super User
Posts: 5,081

Re: %IF Statement and macro resolution

It looks like a logic error.  Your %IF condition must always be true.  Change "or" to "and".

View solution in original post


All Replies
Solution
‎02-28-2017 04:49 PM
Super User
Posts: 5,081

Re: %IF Statement and macro resolution

It looks like a logic error.  Your %IF condition must always be true.  Change "or" to "and".

Frequent Contributor
Posts: 75

Re: %IF Statement and macro resolution

You need an 'and', not an 'or' in this statement:

 

%if &var ne hand and &var ne eye %then %do;

Frequent Contributor
Posts: 75

Re: %IF Statement and macro resolution

The way you have it currently written with an 'or', any one of the two conditions needs to be true for the statement inside to execute. When you pass in hand, &var ne eye is true, so it executes. When you pass in eye, &var ne hand is true so it executes. What you intend to say is execute code only if both conditions are true - not hand AND not eye.

 

%if &var ne hand and &var ne eye %then %do;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 156 views
  • 0 likes
  • 3 in conversation