SAS中的Nullif语句

时间:2019-02-25 23:07:57

标签: sas case nullif

我有一张下表

Id   Values    
 1     A          
 1     A          
 1     B          
 2     @          
 2     @
 2     @
 3     A
 3     A
 3     A
 3     A
 3     @
 4     B
 4     B
 4     B

输出:

Id   Values
 1     @
 2     @
 3     A
 4     B

在每个Id组中,如果所有值均为@,我想将该Id的值设置为@,否则,如果Id的所有值均相同(忽略@s)(例如,所有As),则为该Id设置值设为该值(A),否则将id的值设置为@。

此问题已在sqlserver中回答,我正在尝试在SAS中复制代码,我需要在SAS中执行此操作。但是以某种方式,SAS中的NULLIF无法正常工作。有人可以指导我如何在SAS中做到这一点吗?

2 个答案:

答案 0 :(得分:0)

一种方法是计算组中distinct个值的数量,然后在只有一个值的情况下将结果分配给max(value),否则将其分配给@

 proc sql;
   create table want as
   select id,
     case 
       when count(
          distinct 
          case 
            when value ne '@' then value
          end
          ) = 1 then max(value)
       else '@'
     end as value
   from have
   group by id
  ;

答案 1 :(得分:0)

我发现NULLIF包含在Fedsql Proc中,当我运行以下代码时,它就起作用了。

proc fedsql;
select  id,
case    when    min(NULLIF(values, '@')) = max(NULLIF(values, '@'))
        and     min(NULLIF(values, '@')) ^= '@'
        then    min(NULLIF(values, '@'))
        else    '@'
        end as result
        from    mytable
        group by id;
run;