根据其他列中的观察结果创建一个新列

creating a new column based on observations in other columns

我在使用条件创建新变量时遇到问题,我尝试了数据步骤但无济于事。

我的数据集现在看起来像这样:

A B C D E
1 . 1 1 .
. 1 . . . 
1 . 1 . 1 

我需要看起来像这样

A B C D E R
. .  .  . 1
. 1 . . . .
. . . . . 1

所以我使用的想法是,如果 a - d 的总和大于 1,则将 R 设置为等于 1,否则。然后如果 1 出现在 a & b & c & d & e 中但它没有为我做这可能是由于缺少值,则放弃观察。

到目前为止我使用的代码:

data campZ;
set campY;
select;
when (sum(Macroscopic -- Symbolic > 1)) Random = 1;
otherwise; end; 
run;

我也尝试过 Proc SQL,但我一直主要关注数据步骤,但任何帮助都会很棒。

谢谢!

SELECT A, B, C, D, E,
       CASE WHEN A+B+C+D > 1 THEN 1 END AS R
FROM   Table;

(抱歉,如果我有任何语法错误,我的 SAS SQL 有点生疏。)

您可以执行查询来执行此操作。 . .虽然我认为一个数据步骤是相当合理的。这是在 proc sql.

中执行上述操作的一种方法
proc sql
    select (case when cnt <= 1 then a end) as a,
           (case when cnt <= 1 then b end) as b,
           (case when cnt <= 1 then c end) as c,
           (case when cnt <= 1 then d end) as d,
           (case when cnt <= 1 then e end) as e,
           (case when cnt > 1 then 1 end) as r
    from (select z.*,
                 ((case when a is null then 0 else 1 end) +
                  (case when b is null then 0 else 1 end) +
                  (case when c is null then 0 else 1 end) +
                  (case when d is null then 0 else 1 end) +
                  (case when e is null then 0 else 1 end)
                 ) as cnt

          from campz z
         ) z ;

这只是 returns 值。如果您希望它们在新数据集中,请使用 create table as.

您似乎既要设置 R 又要清除其他变量。当使用变量列表作为函数的参数时,需要添加 OF 关键字。

data campZ;
  set campY;
  if sum(of Macroscopic -- Symbolic) > 1 then do;
     Random = 1;
     call missing(of Macroscopic -- Symbolic);
  end;
run;

数据一;
输入 A B C D E;
卡片;
1. 1 1 .
. 1. . .
1. 1. 1
;

proc sql noprint;
创建 table a1 为
select *, 案例
当 sum(a,b,c,d,e)>1 时 1
当 sum(a,b,c,d,e)<=1 时 .
从 a;
结束为 R 更新 a1 集合 A=., B=., C=., D=., E=.
其中 R=1;
退出;

输出

观察 A B C D E R
1. . . . . 1
2. 1. . . .
3. . . . . 1