首页 > 解决方案 > 基于变量有条件地检索数据

问题描述

我有以下数据集:

have

data have;
infile datalines delimiter=",";
input id $ nbtimes $ typeof $ dp $;
datalines4;
ABC,1,GROUP,A
ABC,1,GROUP,B
ABC,2,COMPANY,A
ABC,2,COMPANY,B
DEF,1,COMPANY,A
DEF,1,COMPANY,B
;;;;
run;

如果 ID 报告“Group”和“Company”,我想输出该实体的数据typeof= "Group";如果 ID 仅报告“公司”,我只想检索具有typeof= "Company".

我希望能够通过proc sql

want

ID nbtimes 类型 类型
美国广播公司 1 团体 一个
美国广播公司 1 团体
国防军 1 公司 一个
国防军 1 公司

标签: sqlsas

解决方案


使用 SQL 创建满足 typeof 的数据集,然后将其与原始数据集合并。根据您提供的条件,ABC 有两种不同的 typeof,包括 'GROUP',DEF 有一种,所以这里是程序:

proc sql;
   select a.* from have a right join (
      select distinct ID,case when count(distinct typeof)>1 then 'GROUP'
      else 'COMPANY' end as typeof
      from have group by ID
    ) b
    on a.ID=b.ID and a.typeof=b.typeof;
quit;

推荐阅读