交织 SAS 数据集(按常见患者编号)

Interleaving SAS Data Sets (by common patient number)

我需要交错访问 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;

如果任一数据集上只有一行,这在数据步骤中很容易做到。

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;

基本上,如果它不是该 ID 的第一行或最后一行,则您只输出一行 - 如果它在两个数据集中,则为真。如果每个 ID 在任一数据集中有不止一行,这将不起作用。

如果您在一个或两个数据集中的每个 ID 都有重复项,那么您还有许多其他解决方案。这是与您的 MERGE idea 最相似的一个。

在 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;