IFC and IFN Functions in SAS

When an expression is evaluated using IFC and IFN functions, a true or false value is returned based on the condition.

IFC and IFN Functions

IFC returns a character value, while IFN returns a numeric value. If you use the IFC function, you need to specify the length of the character variable; otherwise, 200 is the default length.

IFC Function in SAS

With IFC, you can select from several values depending on the value of a logical expression.

Syntax:

IFC( logical-expression, 
value-returned-when-true, 
value-returned-when-false,
value-returned-when-missing)

The first argument, logical expression, is evaluated by IFC. First, the value in the second argument is returned if the logical expression is true (that is, not zero or missing).

Next, IFC returns the value in the fourth argument if a logical expression is missing and you have a fourth argument.

Finally, the third argument is returned if the logical expression is false.

Using IF Then Else and Select When Statemnets

data ex1;
	input x;
	length if_then_var $8;

	if (missing(x)) then
		if_then_var="Missing";
	else if (x) then
		if_then_var="True";
	else
		if_then_var="False";
	length select_var $8;

	select;
		when (x) select_var="True";
		when (missing(x)) select_var="Missing";
		otherwise select_var="False";
	end;
	datalines;
1
0
3
.
8
0
;
run;

Results:

IFC Function in SAS
Result of IF-Then-Else and Select Statements

Using IFC Function

This single assignment statement takes the place of a whole block of IF…THEN…ELSE logic makes the logic clear.

data ex1;
	input x;
	ifcResults1=ifc(x, "True", "False", "Missing");
	/*IFC Without Missing Argument*/
    ifcResults2=ifc(x, "True", "False");
	datalines;
1
4
3
.
8
0 
;
run;
Result of IFC function
Result of IFC Function

IFN Function in SAS

The IFN function uses conditional logic, which allows you to choose between many values based on the value of a logical expression.

IFN evaluates the first argument, followed by the logical expression. Then, IFN returns the value in the second argument if the logical expression is true (that is, not zero or missing).

If the logical expression is null and there is a fourth argument, IFN returns the value in the fourth argument.

Otherwise, IFN returns the value in the third argument if the logical expression is false.

data ex2;
	input x;
	ifnResults2=ifn(x, 1, 0, .);
	ifnResults3=ifn(x,x*10, 0, .);
	/*IFN Without Missing Argument*/
	ifnResults4=ifn(x,x*10, 0);
	datalines;
1
4
3
.
8
0
;
run;
Result of IFN function
Result of IFN Function

The IFC and IFN functions might be misleading regarding missing values.

In the following example, variable x is compared with 5, which is used for tests of logical expressions in the functions.

data ex1;
	input x;
	ifcResults1=ifc(x<5, "True", "False", "Missing");
	ifnResults1=ifn(x<5, 1, 0, .);
	datalines;
1
4
3
.  
8
0
;
run;
proc print;
IFC Function in SAS

The results are as expected. Note that (x < 5 ) does not resolve to missing for observation 4, even if x is missing.

As described in the Order of Missing Values and SAS Operators In Expressions, a missing value for a numeric variable is smaller than all numbers.

Consequently, if you sort your data set by a numeric variable, observations with missing values for that variable appear first in the sorted data set. You can compare special missing values with numbers and each other for numeric variables.

This is. However one case, this is not true: in using the IFC or IFN functions. Hence, the expression (x<5) will never evaluate as missing (since x<5 evaluates to true for the missing).

A word of caution about IFC and IFN

Even though the IFC and IFN functions can give you a lot of options, there is one thing you should be aware of. Before the logical test is done, the arguments given to IFC or IFN are evaluated.

This can cause problems if one of the arguments is invalid under certain conditions, even if the logic is written to keep that argument from being returned under those same conditions.

For example, look at this part of a DATA step with an IF…THEN…ELSE statement:

data ex3;
x=0;
if x ne 0 then y=10/x;
else y=0;
run;

Note that this code was designed to avoid the possibility of division by zero. Using the IFN function, we can write this code in the following way:

y = ifn(x ne 0, 10/x, 0);

No matter what x is, the IFN function will first evaluate 10/x, resulting in a zero division when x equals 0. Even though y will still have the correct value, we will see the following note in our SAS log:

NOTE: Division by zero detected at line 71 column 19.

In this case, we can avoid this problem by using the DIVIDE function, which handles division by zero gracefully by returning a missing value when it is attempted.

y = ifn(x ne 0, divide(10,x), 0);

Here is another example: the SUBSTR function extracts 3 characters only if the var is not blank.

data ex4;
  var = " ";
  length result $7;
  result = ifc(not missing(var), substr(var, 1, 3), "");
run;

In this case, the argument to the SUBSTR function is invalid, so you see a note in the SAS log.

NOTE: Invalid third argument to function SUBSTR at line 72 column 34.

SAS will start evaluating in the following order:

  • not missing(var), which results in false
  • substr(var, 1, 3), which gives an error
  • "", which results in a null string

So the error occurs before the IFC is executed.

In this example, the SUBSTRN function can avoid the error. The starting position and the length arguments of the SUBSTRN function can be 0 or negative without causing an error.

 result = ifc(not missing(var), substrn(var, 1, 3), "");

However, in practice, preventing such issues is not always so simple. So, there could be times when you shouldn’t use the IFC or IFN function.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.