SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 669|回复: 0
打印 上一主题 下一主题

IFN Function

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2012-3-17 22:41:38 | 只看该作者

IFN Function

Returns a numeric value based on whether an expression is true, false, or missing.
Syntax
IFN(logical-expression, value-returned-when-true, value-returned-when-false <,value-returned-when-missing>)  


Arguments


logical-expression
specifies a numeric constant, variable, or expression.

value-returned-when-true
specifies a numeric constant, variable, or expression that is returned when the value of logical-expression is true.

value-returned-when-false
specifies a numeric constant, variable, or expression that is returned when the value of logical-expression is false.

value-returned-when-missing
specifies a numeric constant, variable or expression that is returned when the value of logical-expression is missing.


--------------------------------------------------------------------------------

Details


The IFN function uses conditional logic that enables you to select among several values based on the value of a logical expression.

IFN evaluates the first argument, then logical-expression. If logical-expression is true (that is, not zero and not missing), then IFN returns the value in the second argument. If logical-expression is a missing value, and you have a fourth argument, then IFN returns the value in the fourth argument. Otherwise, if logical-expression is false, IFN returns the value in the third argument.

The IFN function, an IF/THEN/ELSE construct, or a WHERE statement can produce the same results (see Examples). However, the IFN function is useful in DATA step expressions when it is not convenient or possible to use an IF/THEN/ELSE construct or a WHERE statement.


--------------------------------------------------------------------------------

Comparisons


The IFN function is similar to the IFC function, except that IFN returns a numeric value whereas IFC returns a character value.


--------------------------------------------------------------------------------

Examples




Example 1: Calculating Sales Commission
The following examples show how to calculate sales commission using the IFN function, an IF/THEN/ELSE construct, and a WHERE statement. In each of the examples, the commission that is calculated is the same.



Calculating Commission Using the IFN Function
In the following example, IFN evaluates the expression TotalSales > 10000 . If total sales exceeds $10,000, then the sales commission is 5% of the total sales. If total sales is less than $10,000, then the sales commission is 2% of the total sales.

data _null_;
   input TotalSales;
   commission=ifn(TotalSales > 10000, TotalSales*.05, TotalSales*.02);
   put commission=;
   datalines;
25000
10000
500
10300
;

run;

SAS writes the following output to the log:

commission=1250
commission=200
commission=10
commission=515
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-6 17:58 , Processed in 0.076791 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表