07-15-2013 06:14 AM
I am having an issue with the IFC and IFN functions using SAS version 9.1.3.
Based on the SAS documentation, these functions can be used to return 3 different values.
The syntax is written as:
IFC(logical-expression, value-returned-when-true, value-returned-when-false ,<value-returned-when-missing>)
Just as an example here is some code I have tried out.
I have created the class dataset:
input id name $5-9;
Then I create the class2 dataset, and use the IFC function to try assign 3 different values for a new variable
The created dataset looks like this:
The first 2 records look as I expected. However I was expecting the 3rd record to have the answer 'Missing' instead of 'No'. Because this record has a blank value for the variable name
I have tried out various ways of using the IFC and IFN functions, but so far have never managed to get the 3rd expression working to cover missing values
Does anyone know what I am doing wrong?
Or if there is a problem with these functions?
Or has an example of how to correctly use these functions to return seperate values for true, false and missing?
Hope that all makes sense! And ideas would be greatly appreciated
07-15-2013 07:14 AM
It works in the following instance with numeric and character data, but I can't get it to work for with N=..
attrib N length = 4
Value length = $ %length(missing);
do N = ., 0, 1, -1;
Value = ifc(N
07-15-2013 09:03 AM
The confusion lies in what IFC examines to determine true / false / missing. It is NOT looking at the value of NAME. Rather, it is looking at the truth / falsity of the first argument (name='Henry'). When NAME is Henry, the comparison returns a 1 which SAS interprets as true. When NAME is anything else (including a blank), the comparison returns a 0 which SAS interprets as false.
As Scott's example illustrates, the first argument would have to return a missing value in order for the fourth argument to kick in. But a comparison as the first argument will always return 1 or 0.
07-15-2013 01:40 PM
Agree with Astounding's nice explanation.
Curious if folks see much benefit to SAS having implemented IFC and IFN with such optional trinary logic?
Just about everywhere else I can think of in SAS, it uses binary logic. Even in PROC SQL, where many SQL implementations use trinary logic (treating mssings as unknown rather than false), SAS uses binary logic.
I think the the fourth parameter to IFC/IFN probably causes more confusion than it is worth. Many people familiar with SAS will not expect it to be there, and folks reading the docs quickly will think that it will work as David expected.
07-16-2013 03:03 AM
Thanks everyone for your replies! That was exactly what I needed to know, I definitely understand the IFC/IFN functions better now