首页 > 解决方案 > 根据列值在 SAS 中添加条目

问题描述

如果特定主题及其各自测试的“计数”列中没有值 1 的条目,我正在尝试向 SAS 数据集添加一个额外的条目。该条目将添加“测试”列值,以及“值”列的“缺失”和计数的 1。

这是数据:

Subject            Test               Value                 Count
001                Test1              Normal                0
001                Test2              Normal                0
001                Test2              High                  1
002                Test1              Normal                0
002                Test2              Normal                0
002                Test2              Normal                1

我想创建以下数据集:

Subject            Test               Value                 Count
001                Test1              Normal                0
001                Test1              Missing               1
001                Test2              Normal                0
001                Test2              High                  1
002                Test1              Normal                0
002                Test1              Missing               1
002                Test2              Normal                0
002                Test2              Normal                1

任何见解将不胜感激。

标签: sqlsas

解决方案


我只会使用union all

proc sql;
    select subject, test, value, count
    from data d
    union all
    select subject, test, 'Missing', 1
    from data d
    group by subject, test
    having max(count) = 0;

推荐阅读