IFC and IFN functions: New IF functions:

Objective: To Reduce the amount of typing required achieving an objective

Syntax: IFN (condition, true, false, missing): ‘N’ stands for Numeric
IFN returns a numeric value. It returns the true, false or missing value depending on whether the condition is true, false or missing.

Syntax: IFC (condition, true, false, missing): ‘C’ stands for character
IFC function has four parameters:
1) a logical expression
2) character value returned when true
3) value returned when false
4) value retuned when missing, which is optional.

IFC (logical-expression, Character-value-returned-when-true, Character-value-returned-when-false, Character-value-returned-when-missing);
IFC returns a character value. It returns the true, false or missing value depending on whether the condition is true, false or missing.

Example: Assign a value to the VISIT variable (new) as per the VTYPE variable value.
We can certainly achieve this task in diff. ways.. here are they...

data old;
input sitesub $ vtype vdate $;
cards;
01-303 1.4 12/23/2005
01-304 1.5 09/03/2005
01-305 1.4 10/09/2005
01-306 1.5 11/17/2005
01-307 1.5 05/29/2005
01-308 . 04/30/2005
;
run;

1) * Using Proc Format:
proc format;
value vt
1.4='Baseline'
1.5='Retreat'
.='Missing'
;
run;
data new;
set old;
length visit $20;
visit=put(vtype,vt.);
run;

2) *Using the IF-THEN/ELSE statements;
data new;
set old;
length visit $20;
if vtype=1.4 then visit='Baseline';
else if vtype=1.5 then visit='Retreat';
else if vtype=. then visit='Missing';
run;

3) *Using the Proc SQL;
Proc sql;
Create table new4 as
Select *,
case
when vtype=1.4 then 'Baseline'
when vtype=1.5 then 'Retreat'
else 'Missing'
end as visit
from old;
Quit;

All three above methods required significant amount of typing when we compared with the below ones......
*Using the IFC function in Datastep;
data new;
set old;
length visit $20;
visit=ifc(vtype=1.4,'Baseline','Retreat','Missing');
run;


*Using the IFC function in Proc SQl;
Proc sql;
Create table new as
Select *,
Ifc(vtype=1.4,'Baseline','Retreat','Missing') as visit
from old;
Quit;

In a DATA step, if the IFC function returns a value to a variable that has not previously been assigned a length, then that variable is given a length of 200 bytes.

Note: IFN and IFN functions cannot be used if we want to assign more than 3 values (including missing) to a variable.

Comments

  1. hi

    Here if we use "ifc" function thats not checking for missing values its giving the same format to missing also. Can you please check and tell

    Thanks & REgards
    chandra

    ReplyDelete
  2. Hi,

    Your example is simple and straigthforward - thanks for posting it. Two minor points:

    1) I think your example should use the IFN function rather than IFC since VTYPE is numeric

    2) The handling of missing values seems incorrect since, using SAS 9.2, the last observation gets the value RETREAT

    Jean Hardy
    Services Conseils Hardy
    jhardy@schardy.qc.ca

    ReplyDelete
  3. Hi
    1. IFC was the right function to use as it addresses the results and not the condition, which can use numeric or character.
    2. The function does not handle the missing value, which is taken as a "false" through the condition.
    Marc Letourneau
    Chiltern
    marc.letourneau@chiltern.com

    ReplyDelete
  4. Is it possible to have multiple conditions within the IFC function?

    ReplyDelete
  5. Yes, it is possible to have multiple conditions within the IFC function in SAS. The IFC function is a conditional function that returns different values depending on whether a specified condition is true or false. However, when you have multiple conditions, you can combine them using logical operators like AND (&) or OR (|).

    Example with multiple conditions:
    sas
    Copy code
    data example;
    input score1 score2;
    result = ifc(score1 > 50 and score2 > 50, "Pass", "Fail");
    result2 = ifc(score1 > 50 or score2 > 50, "Conditional Pass", "Fail");
    datalines;
    55 60
    45 70
    40 30
    ;
    run;

    proc print data=example noobs;
    run;
    Explanation:
    Condition 1 (score1 > 50 and score2 > 50):

    If both conditions are true, the result column will be "Pass".
    If either condition is false, the result column will be "Fail".
    Condition 2 (score1 > 50 or score2 > 50):

    If either condition is true, the result2 column will be "Conditional Pass".
    If both conditions are false, the result2 column will be "Fail".
    Key Points:
    Use AND (&) to ensure all conditions must be true.
    Use OR (|) to allow any one of the conditions to be true.
    You can nest multiple conditions or use parentheses to group conditions for clarity.
    This flexibility makes the IFC function powerful for implementing conditional logic directly within a single statement.

    ReplyDelete

Post a Comment

Popular posts from this blog

SAS Interview Questions and Answers: CDISC, SDTM and ADAM etc

Comparing Two Methods for Removing Formats and Informats in SAS: DATA Step vs. PROC DATASETS

Studyday calculation ( --DY Variable in SDTM)