If statement SAS保留所有,否则

If statement SAS保留所有,否则,if-statement,sas,If Statement,Sas,我有一个sas数据集,如下所示: ID Day Instance1 Instance2 1 1 N Y 1 2 N Y 1 3 Y N 1 4 N N 2 1 N Y 2 2 N N 2 3 N

我有一个sas数据集,如下所示:

ID    Day    Instance1  Instance2
1      1         N         Y
1      2         N         Y
1      3         Y         N
1      4         N         N
2      1         N         Y
2      2         N         N
2      3         N         Y  
2      4         N         N
我想根据它们是否被标记为yes来保存实例,哪怕只有一次。否则将标记为“否”。我希望的输出为:

ID   Instance1  Instance2
1         Y         Y
2         N         Y
我尝试的是伪代码:

DATA test, 
    set.test, 
    by ID;
       if all instance1 = N then N, else yes; 
       if all instance2 = N then N, else yes;
RUN; 
这应该行得通

proc sql;
select distinct id,  max(instance1) as instance1_max ,
max(instance2) as instance2_max
from have
group id
having instance1 =max(instance1)
or instance2 = max(instance2);
或者通过数据步骤

proc sort data=have out=have1;
by id;
run;

data want(rename = (instance1_final = instance1 instance2_final = instance2));
do until(last.id);
set have1;
by id;
if instance1 ='Y' then instance1_final ='Y';
if instance1_final = ' ' then instance1_final='N';
if instance2 ='Y' then  instance2_final ='Y';
if instance2_final = ' ' then instance2_final='N';
end;
drop instance1 instance2 Day;
if instance1_final = "Y" or  instance2_final = "Y";
run;
这应该行得通

proc sql;
select distinct id,  max(instance1) as instance1_max ,
max(instance2) as instance2_max
from have
group id
having instance1 =max(instance1)
or instance2 = max(instance2);
或者通过数据步骤

proc sort data=have out=have1;
by id;
run;

data want(rename = (instance1_final = instance1 instance2_final = instance2));
do until(last.id);
set have1;
by id;
if instance1 ='Y' then instance1_final ='Y';
if instance1_final = ' ' then instance1_final='N';
if instance2 ='Y' then  instance2_final ='Y';
if instance2_final = ' ' then instance2_final='N';
end;
drop instance1 instance2 Day;
if instance1_final = "Y" or  instance2_final = "Y";
run;

您还可以使用另一种使用滞后变量的方法。这将与上一个值匹配,最后一次观察将具有所需的变量

data test;
input Id 1. I1 $1. I2 $1.;
datalines;
1NY
1NY
1YN
1NN
2NY
2NN
2NY  
2NN
;
run;

proc sort data=test; by Id I1 I2; run;

data test1;
 set test;
  by Id I1 I2;
  if I1='Y' or lag(I1)='Y' then Ins1='Y';
  else Ins1='N';
  if I2='Y' or lag(I2)='Y' then Ins2='Y';
  else Ins2='N';
  if last.Id;
  drop I1 I2;
run;

proc print data=test1; run;

您还可以使用另一种使用滞后变量的方法。这将与上一个值匹配,最后一次观察将具有所需的变量

data test;
input Id 1. I1 $1. I2 $1.;
datalines;
1NY
1NY
1YN
1NN
2NY
2NN
2NY  
2NN
;
run;

proc sort data=test; by Id I1 I2; run;

data test1;
 set test;
  by Id I1 I2;
  if I1='Y' or lag(I1)='Y' then Ins1='Y';
  else Ins1='N';
  if I2='Y' or lag(I2)='Y' then Ins2='Y';
  else Ins2='N';
  if last.Id;
  drop I1 I2;
run;

proc print data=test1; run;

使用RETAIN语句相当简单。在这里,它是以一种相当详细的方式来清楚地显示发生了什么

DATA test;
set test;
by ID;
retain instance1Y instance2Y;

if first.ID then do;
  instance1Y=instance1;
  instance2Y=instance2;
end;

if instance1='Y' then instance1Y='Y';
if instance2='Y' then instance2Y='Y';

if last.ID then do;
  instance1=instance1Y;
  instance2=instance2Y;
  output;
end;
run;

使用RETAIN语句相当简单。在这里,它是以一种相当详细的方式来清楚地显示发生了什么

DATA test;
set test;
by ID;
retain instance1Y instance2Y;

if first.ID then do;
  instance1Y=instance1;
  instance2Y=instance2;
end;

if instance1='Y' then instance1Y='Y';
if instance2='Y' then instance2Y='Y';

if last.ID then do;
  instance1=instance1Y;
  instance2=instance2Y;
  output;
end;
run;

代码的结果将为两个ID的两个实例生成“N”。如果ID中只有一个Y,我怎么能让它产生Y呢?@Serenale这正是它所做的。retain语句保存instance1Y和instance2Y中的“Y”值。我刚刚运行了它,它产生了您想要的输出。谢谢您的回复。我可能遗漏了一些东西,但当我在使用上述Rhythm的测试数据后运行代码时,我得到了所有的“N”。您的代码的结果将为两个ID的两个实例产生“N”。如果ID中只有一个Y,我怎么能让它产生Y呢?@Serenale这正是它所做的。retain语句保存instance1Y和instance2Y中的“Y”值。我刚刚运行了它,它产生了您想要的输出。谢谢您的回复。我可能遗漏了一些东西,但当我在使用上述Rhythm的测试数据后运行代码时,我得到了所有的“N”。