SAS如何验证值是否存在

时间:2016-05-26 12:38:08

标签: sas exists

在SAS中,我想通过以下标准定义变量new_var

对于每个id,如果存在reference='A',则为new_var='A'; else new_var='B',例如:

id  reference  new_var
--  ---------  -------
1   A          A
1   B          A
1   C          A
2   B          B   
2   B          B   

提前致谢!

2 个答案:

答案 0 :(得分:2)

您可以在id级别的基础上将引用为A的观察结果加入原始数据集:

data have;
   input id $ reference $;
   datalines;
     1       A
     1       B
     1       C
     2       B
     2       B  
    ;
run;

proc sql;
    create table want as select
        a.id, a.reference,
        case when b.reference = "A" then "A" else "B" end as new_var
        from have as a
        left join have (where = (reference = "A")) as b
        on a.id = b.id
        order by a.id, a.reference;
quit;

答案 1 :(得分:1)

如果数据与您的示例一样简单,并按ID和引用排序,那么只需检查id中每个更改的参考值(使用first.id)。 retain语句为具有相同id的每个后续行复制new_var值。

如果检查的参考值并非总是按字母顺序排在第一位,则此方法无法正常工作。

data have;
input id reference $;
datalines;
1       A
1       B
1       C
2       B
2       B
;
run;

data want;
set have;
by id reference;
retain new_var;
if first.id then do;
    if reference='A' then new_var='A';
    else new_var='B';
end;
run;