我需要交错到 SAS 数据集,但前提是它们都存在患者 ID。在合并语句中,我会使用“in”和“if”,但是,我需要堆叠数据。数据在变量方面是等价的。
有任何想法吗?
我需要交错到 SAS 数据集,但前提是它们都存在患者 ID。在合并语句中,我会使用“in”和“if”,但是,我需要堆叠数据。数据在变量方面是等价的。
有任何想法吗?
这有点麻烦,但如果数据集相同,那么您可以尝试以下方法。假设您在变量 ID 上匹配。
proc sql;
select t1.*
from
TABLE_A t1
where ID in (select ID from TABLE_B)
union all
select t2.*
from
TABLE_B t2
where ID in (select ID from TABLE_A)
;quit;
If you have precisely one row on either dataset, this is fairly easy to do in the data step.
data have_1;
do id = 1 to 20 by 2;
output;
end;
run;
data have_2;
do id = 1 to 20 by 3;
output;
end;
run;
data want;
set have_1 have_2;
by id;
if not (first.id and last.id);
run;
Basically, you only output a row if it is not the first or not the last row for that ID - which will be true iff it is in both datasets. This doesn't work if you have more than one row in either dataset per ID.
如果您在一个或两个数据集中的每个 ID 都有重复项,那么您还有很多其他解决方案。这是与您的 MERGE 想法最相似的一个。
在 Double DoW 循环中,您循环遍历数据集两次,一次检查您的条件,然后一次实际输出。这使您可以查看每个 ID 的所有行,查看您的条件是否有效,然后再次查看所有行以针对该条件采取行动。
data have_1;
do id = 1 to 20 by 2;
output;
output;
end;
run;
data have_2;
do id = 1 to 20 by 3;
output;
output;
end;
run;
data want;
_a=0; *initialize temporary variables;
_b=0; *they will be cleared once for each ID;
do _n_ = 1 by 1 until (last.id);
set have_1(in=a) have_2(in=b);
by id;
if a then _a=1; *save that value temporarily;
if b then _b=1; *again temporary;
end;
do _n_ = 1 by 1 until (last.id);
set have_1 have_2;
by id;
if _a and _b then output; *only output the rows that have both _a and _b;
end;
run;