[英]SAS: select groups of observations based on values in two columns and multiple rows
In SAS, I need to select subjects and their data rows based on values in two variables across several rows. 在SAS中,我需要根据跨多个行的两个变量中的值来选择主题及其数据行。 In the data below ID is the relevant BY group.
在下面的数据中,ID是相关的BY组。 I need to output the group of rows associated with a person who has X in (0,1,9) and Y=missing on all rows.
我需要输出与在(0,1,9)中具有X并且在所有行上均缺少Y的人相关联的行组。 Therefore no rows would be outputted for ID=01 because it has an X=1 and non-missing Y in two other rows.
因此,对于ID = 01不会输出任何行,因为它在其他两行中具有X = 1且不丢失Y。 Two rows must be output for ID=02 and ID=03.
必须为ID = 02和ID = 03输出两行。 And the row for ID=04 must be output.
并且必须输出ID = 04的行。 Thanks.
谢谢。
ID X Y
01 1 .
01 . 1
01 . 1
02 0 .
02 . .
03 9 .
03 . .
04 1 .
Try this: 尝试这个:
data have;
input ID $ X Y;
cards;
01 1 .
01 . 1
01 . 1
02 0 .
02 . .
03 9 .
03 . .
04 1 .
;
proc sql;
select * from have group by id having x in(0,1,9) and sum(y) is null;
quit;
data have;
input ID $ X Y;
cards;
01 1 .
01 . 1
01 . 1
02 0 .
02 . .
03 9 .
03 . .
04 1 .
;
run;
proc sort data=have;
by id;
run;
data list;
set have;
by id;
retain keepit;
if first.id then keepit = .;
if missing(keepit) or keepit=1 then do;
if missing(y) then do;
if x in (0,1,9) then keepit = 1;
end;
else keepit = 0;
end;
if last.id and keepit then output;
keep id;
run;
data want;
merge
have (in=a)
list (in=b)
;
by id;
if a and b;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.