If statement SAS中同一变量的多个条件
我试图检测一个变量的特定值,并在满足这些条件的情况下创建一个新的变量 以下是我的部分数据(我有更多的行): 我想要什么If statement SAS中同一变量的多个条件,if-statement,sas,If Statement,Sas,我试图检测一个变量的特定值,并在满足这些条件的情况下创建一个新的变量 以下是我的部分数据(我有更多的行): 我想要什么 id time result base 1 1 normal 1 2 normal x 1 3 abnormal 2 1 normal x 2 2 2 3 normal 3 3 normal 4 1 normal 4 2 normal x 4
id time result base
1 1 normal
1 2 normal x
1 3 abnormal
2 1 normal x
2 2
2 3 normal
3 3 normal
4 1 normal
4 2 normal x
4 3 abnormal
5 1 normal
5 2 normal x
5 3
当结果存在于时间点(时间)2时,应填充我的基线值(基准)。如果没有结果,那么基线应该在时间=1
if result="" and time=2 then do;
if time=10 and result ne "" then base=X; end;
if result ne "" and time=2 then base=X; `
当时间=2且结果存在时,它可以正常工作。但如果结果不见了,那就有问题了 这个问题似乎有点离题。“否则,如果time=”“和time=1”似乎在某个地方有输入错误 但是,您的语法似乎很可靠。我已经用你提供的数据做了一个例子。第一个条件有效,但第二个条件(否则)是假设。随着问题的更新而更新
options missing='';
data begin;
input id time result $ 5-20 ;
datalines;
1 1 normal
1 2 normal
1 3 abnormal
2 1 normal
2 2
3 3 normal
4 1 normal
4 2 normal
4 3 abnormal
;
run;
data flagged;
set begin;
if time=2 and result NE "" then base='X';
else if time=1 and id=2 then base='X';
run;
根据重新访问的问题进行编辑
假设时间点(1)总是紧挨着时间点(2)。(如果没有,则添加更多滞后)。模拟Lead函数,我们将数据向后排序并利用滞后
proc sort data=begin; by id descending time; run;
data flagged;
set begin;
if lag(time)=2 and lag(result) EQ "" then base='X';
if time=2 and result NE "" then base='X';
run;
更多关于滞后的反面:这个问题似乎有点离题。“否则,如果time=”“和time=1”似乎在某个地方有输入错误 但是,您的语法似乎很可靠。我已经用你提供的数据做了一个例子。第一个条件有效,但第二个条件(否则)是假设。随着问题的更新而更新
options missing='';
data begin;
input id time result $ 5-20 ;
datalines;
1 1 normal
1 2 normal
1 3 abnormal
2 1 normal
2 2
3 3 normal
4 1 normal
4 2 normal
4 3 abnormal
;
run;
data flagged;
set begin;
if time=2 and result NE "" then base='X';
else if time=1 and id=2 then base='X';
run;
根据重新访问的问题进行编辑
假设时间点(1)总是紧挨着时间点(2)。(如果没有,则添加更多滞后)。模拟Lead函数,我们将数据向后排序并利用滞后
proc sort data=begin; by id descending time; run;
data flagged;
set begin;
if lag(time)=2 and lag(result) EQ "" then base='X';
if time=2 and result NE "" then base='X';
run;
更多关于滞后的反面:请使您的示例数据与示例代码保持一致。在代码中,您引用了样本数据中未出现的变量
visit
、visitnum
和day
。您的标志变量在数据中称为base
,但在代码中称为base
。除此之外,从您的示例结果和小的解释(看起来不正确)来看,还不清楚您想要实现什么。你的if-then-if
语法看起来很奇怪。那个代码被抄得如此匆忙,我甚至没有意识到有这么多错误。现在我已经更正并修改了我的问题。请使您的示例数据与示例代码一致。在代码中,您引用了样本数据中未出现的变量visit
、visitnum
和day
。您的标志变量在数据中称为base
,但在代码中称为base
。除此之外,从您的示例结果和小的解释(看起来不正确)来看,还不清楚您想要实现什么。你的if-then-if
语法看起来很奇怪。那个代码被抄得如此匆忙,我甚至没有意识到有这么多错误。现在我已经更正并修改了我的问题。是的,这对这4个主题有效,但实际上在我的数据集中有60个主题(一开始没有提到),所以用id号检测这些值会让人筋疲力尽。@Laura更新了答案。嗨!行了。非常感谢你!是的,这适用于这4个主题,但实际上在我的数据集中有60个主题(一开始没有提到),所以用id号检测这些值会让人筋疲力尽。@Laura更新了答案。嗨!行了。非常感谢你!