Saturday, June 20, 2009

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.

3 comments:

chandrra said...

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

Anonymous said...

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

Unknown said...

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

Post a Comment

ShareThis