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!
Editor's note: Tom summed it up well below. Here is a chart I also use to discuss the differences between IF and %IF:
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.
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
IF is used inside a datastep. %IF is used in open code. You can always use %IF instead of IF.
Editor's note: Tom summed it up well below. Here is a chart I also use to discuss the differences between IF and %IF:
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.
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;
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.