[英]Do I need to create variable using if statement in SAS?
I need to write a code in SAS in order to substitute Access.我需要在 SAS 中编写代码以替换 Access。 There's one part of this code that I don't know how to do it due to the difference between Access/SQL and SAS using "if".
由于使用“if”的 Access/SQL 和 SAS 之间的差异,我不知道如何执行此代码的一部分。
While in Access the command "if" doesn't need to create a new variable, in SAS we have to create it.在 Access 中,命令“if”不需要创建新变量,而在 SAS 中我们必须创建它。
In Access, the code is something like this:在 Access 中,代码是这样的:
(IIf(date2 > par_final, par_final, date2) -
IIf(date1 > par_initial, par_initial, date1))/365 as exposure
All the variables are in date format.所有变量都是日期格式。 When I write this in SAS code, I have to create a new variable.
当我用 SAS 代码写这个时,我必须创建一个新变量。 For example:
例如:
If date2 > par_final then
Field1 = par_final; Else Field1 = date2;
However, I want to create Just the exposure.但是,我想创造只是曝光。 I don't need to create any other variables.
我不需要创建任何其他变量。
How can I write this in SAS code?如何在 SAS 代码中编写此代码?
I thought firstly create the first variable using the if condition and also the Second variable using if after minus.我想首先使用 if 条件创建第一个变量,然后在减号之后使用 if 创建第二个变量。 Then I make (field1 - field2)/365 to create exposure and drop field1 and field2.
然后我制作 (field1 - field2)/365 来创建曝光并删除 field1 和 field2。
Could anyone give me a better suggestion?谁能给我一个更好的建议? Are there ways to do it without creating those variables?
有没有办法在不创建这些变量的情况下做到这一点?
Thanks in advance.提前致谢。
One of the divergences of MS Access' SQL dialect from standard SQL is IIF
function (not to be confused with IF
operator) which most database dialects use the CASE
statement.其中的分歧从标准SQL的MS Access' SQL方言是
IIF
函数(不要与混淆IF
运营商)的大多数数据库方言使用CASE
语句。
(IIf(date2 > par_final, par_final, date2) -
IIf(date1 > par_initial, par_initial, date1))/365 as exposure
Therefore, above can be translated with CASE
expressions and should run in any compliant ANSI-92 SQL compiler including SAS's proc sql
:因此,以上可以使用
CASE
表达式进行翻译,并且应该在任何兼容的 ANSI-92 SQL 编译器中运行,包括 SAS 的proc sql
:
(CASE WHEN date2 > par_final THEN par_final ELSE date2 END -
CASE WHEN date1 > par_initial THEN par_initial ELSE date1 END) / 365 AS exposure
SAS has conditional logic functions IFC
and IFN
that return respectively a character or numeric value based on a logical evaluation. SAS 具有条件逻辑函数
IFC
和IFN
,它们根据逻辑评估分别返回一个字符或数值。 SAS date values are serial integers from epoch 01-JAN-1960
so you can continue to use subtraction to compute number of days of exposure. SAS 日期值是
01-JAN-1960
纪元的序列整数,因此您可以继续使用减法来计算暴露天数。
Try replacing尝试更换
(IIf(date2 > par_final, par_final, date2) -
IIf(date1 > par_initial, par_initial, date1))/365 as exposure
with和
( IFN(date2 > par_final, par_final, date2)
-
IFN(date1 > par_initial, par_initial, date1)
) / 365 as exposure label="Years of exposure"
Examples例子
data have;
input ini d1 d2 fin;
datalines;
1 2 3 4
2 1 3 4
1 2 4 3
2 1 4 3
;
* Expression in SQL;
proc sql;
create table foo as
select
( ifn ( d2 > fin, fin, d2 )
-
ifn ( d1 > ini, ini, d1 )
)
as diff label = 'Difference of IFNs'
from have
;
quit;
* Expression in DATA Step;
data foo_also;
set have;
diff = ( ifn ( d2 > fin, fin, d2 )
-
ifn ( d1 > ini, ini, d1 )
);
attrib diff label='Difference of IFNs';
run;
Also还
Try using function MIN
instead of IFN
尝试使用函数
MIN
而不是IFN
exposure = (min(d2,fin) - min(d1,ini)) / 365;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.