Help using Base SAS procedures

When to use %IF and IF in Macros?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

When to use %IF and IF in Macros?

Hi,

I am trying to get a clear picture of when can anyone use %IF and IF in macros.

What kind of situation demands the need of %IF in a macros and IF in macros?

Please share your thoughts.

Thanks!


Accepted Solutions
Solution
Monday
Super User
Super User
Posts: 6,500

Re: When to use %IF and IF in Macros?

[ Edited ]

Editor's note: Tom summed it up well below.  Here is a chart I also use to discuss the differences between IF and %IF:

 

Capture.PNG

This is a good chance for you to internalize the difference between macro statements and normal SAS statement.

In general the macro language is used to generate commands that SAS will run.

  • Macro conditional logic statements like %IF can be used to generate different commands in based on values of macro variables.
  • Normal SAS statements like IF run when the macro has generated a complete data step.  It can be used to execute different data step statements based on the values of the data in the current observation.

 

  • The IF statement is only valid inside of a data step. 
  • The %IF macro statement is only valid inside of a macro. That is you cannot use it in a program without first including it in a macro definition and then calling the macro that you have defined.

View solution in original post


All Replies
Respected Advisor
Posts: 3,124

Re: When to use %IF and IF in Macros?

Simply put, 'if' process data set variables, and results in data set variables; '%if' compares macro variables, results in sending which string of text to SAS compiler.

Haikuo

Occasional Contributor
Posts: 5

Re: When to use %IF and IF in Macros?

IF is used inside a datastep. %IF is used in open code. You can always use %IF instead of IF.

Solution
Monday
Super User
Super User
Posts: 6,500

Re: When to use %IF and IF in Macros?

[ Edited ]

Editor's note: Tom summed it up well below.  Here is a chart I also use to discuss the differences between IF and %IF:

 

Capture.PNG

This is a good chance for you to internalize the difference between macro statements and normal SAS statement.

In general the macro language is used to generate commands that SAS will run.

  • Macro conditional logic statements like %IF can be used to generate different commands in based on values of macro variables.
  • Normal SAS statements like IF run when the macro has generated a complete data step.  It can be used to execute different data step statements based on the values of the data in the current observation.

 

  • The IF statement is only valid inside of a data step. 
  • The %IF macro statement is only valid inside of a macro. That is you cannot use it in a program without first including it in a macro definition and then calling the macro that you have defined.
Occasional Contributor
Posts: 13

Re: When to use %IF and IF in Macros?

Hi Renjith,

At the start you will always behaving a confusion whats the difference between these two. Here is the simple fact:

1.SAS Macro will be executed first and once done normal SAS statements will be executed.

2. IF statement cannot be used outside the data step.

So Just like the IF condition in SAS statement, %IF is used to check if a particular condition is matching. If it is so then execute the SAS statement.

Now Imagine you have a macro variable and based on value of it you want to execute set of SAS statements or in more detail let say u want to create a dataset sumthing like this:

%macro Trial;

%let a=1;

%if &a=1 %then %do;

data temp;

emp=232323;

run;

%end;

%mend;

%trial;

Above if the macro variable A has value one then  the datastep will be send to SAS compiler for execution. If the macro variable value is not equal to one. SAS Macro will hide the data step from SAS compiler. Lets say like information hiding. SAS compiler will never receive that datastep.

IF statement power remains only within the dataset. You cannot use If statement outside the datastep where as %if can be used any where:

but within the %macro  and %mend statement. Like below example:

%let a=2;

data teemp;

a=1;output;

a=2;output;

run;

%macro h;

data temp2l;

set teemp;

%if &a=1 %then %do;

b=1 ; %end;

%else %do;

b=2;

%end;

run;

%mend;

%h;

Contributor
Posts: 30

Re: When to use %IF and IF in Macros?

Some code that might help demonstrate the difference

%macro ifdemo();

data test;

do i=1 to 5;

j=1;

%let k=1;

if j then put "j is not zero or missing";

%if &k %then %put k is not zero or missing;

end;

run;

%mend;

%ifdemo;

When you run this and check the log, you'll see:

k is not zero or missing

j is not zero or missing

j is not zero or missing

j is not zero or missing

j is not zero or missing

j is not zero or missing

What happens here: first, SAS goes through the code looking for macro statements/macro variables and executing/resolving them. In this case, it sets k=1, then tests the value of k and prints to the log. This only happens once - it's not paying any attention to the DO loop at this stage because it's not a macro command.

After it's finished dealing macros, it then goes back and runs the DATA step. Now the i-loop runs and the PUT statement for j executes 5 times.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 10952 views
  • 4 likes
  • 6 in conversation