BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
renjithr
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Haikuo
Onyx | Level 15

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

zilok
Calcite | Level 5

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

Tom
Super User Tom
Super User

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.
rocky1983
Fluorite | Level 6

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;

GeoffreyBrent
Calcite | Level 5

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.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 37543 views
  • 5 likes
  • 6 in conversation