BookmarkSubscribeRSS Feed
davidhw
Calcite | Level 5

Hi all,

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>)

value-returned-when-true specifies a character expression that is returned when the value of logical-expression is true.
value-returned-when-false specifies a character expression that is returned when the value of logical-expression is false.
value-returned-when-missing specifies a character expression that is returned when the value of logical-expression is missing

Just as an example here is some code I have tried out.

I have created the class dataset:

data class;

  input id name $5-9;

  datalines;

  1 Henry

  2 Alice

  3  

  ;

run;

Then I create the class2 dataset, and use the IFC function to try assign 3 different values for a new variable

data class2;

  set class;

  answer=ifc(name='Henry','Yes','No','Missing');

run;

The created dataset looks like this:
class2.png
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

Thanks,

David

4 REPLIES 4
Scott_Mitchell
Quartz | Level 8

It works in the following instance with numeric and character data, but I can't get it to work for with N=..

DATA Work.TestIfc;

attrib N length = 4

Value length = $ %length(missing);

do N = ., 0, 1, -1;

Value = ifc(N

,"true"

,"false"

,"missing"

);

output;

end;

stop;

run;

Astounding
PROC Star

David,

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.

Good luck.

Quentin
Super User

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.

--Q.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
davidhw
Calcite | Level 5

Thanks everyone for your replies! That was exactly what I needed to know, I definitely understand the IFC/IFN functions better now

Thanks again!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 14078 views
  • 9 likes
  • 4 in conversation