SAS的有条件合并?

时间:2012-10-25 19:08:17

标签: sql sas

我有表 a ,一个变量是虚拟变量,“Y”* 或* “N”

如果“N”,请按提供商的电话号码计算独特的患者和独特的提供者;  如果“Y”,则按提供者的ID加入表 b ,然后计算唯一患者和独特提供者。

这是我的代码。这很乏味。我们是否有一个 PROC SQL 步骤来实现我想要的或更简单的方法?谢谢!

data yes no ;
  set a;
  if PRV_SPECIAL_HANDLING='Y' then output yes;
  if PRV_SPECIAL_HANDLING='N' then output no;
run;

proc sort data=yes out=yes1; by prv_id; run;
proc sort data=b nodupkey out=HANDLING; by prv_id; run;

data merge_yes;
  merge yes1(in=a) handling(in=b);
  by prv_id;
  if a;
run;

proc sort data=no out=no1; by prv_id; run;
data final;
  set no1 merge_yes;
  by prv_id;
run;

proc sql;
     create table CN_uni_prv as
     select PROV_PHONE, 
            count(unique(prv_id)) as uni_prv, 
            count(unique(pt_id)) as uni_pt
     from final
     group by PROV_PHONE ;
quit;

2 个答案:

答案 0 :(得分:0)

答案是肯定的。我发现有点难以按照你的表和列。假设提供者集合不同,则对没有电话号码的提供者执行计数,然后将其与您加入第二个表格的计数结合起来。

查询看起来像:

select PROV_PHONE, count(distinct prv_id) as uni_prv, count(distinct pt_id) as uni_pt
from a
where PRV_SPECIAL_HANDLING='N'
group by prov_phone
union all
select PROV_PHONE, count(distinct prv_id) as uni_prv, count(distinct pt_id) as uni_pt
from a join
     b
     on a.prv_id = b.prv_id
where PRV_SPECIAL_HANDLING='Y'
group by prov_phone

如果存在重叠,则需要在组之前执行联合:

select PROV_PHONE, count(distinct prv_id) as uni_prv, count(distinct pt_id) as uni_pt
from ((select PROV_PHONE, prv_id, pt_id
       from a
       where PRV_SPECIAL_HANDLING='N'
       group by prov_phone
      ) union all
      (select PROV_PHONE, b.prv_id, b.pt_id
       from a join
            b
            on a.prv_id = b.prv_id
       where PRV_SPECIAL_HANDLING='Y'
      )
     ) t
 group by prov_phone

我将count(unique)替换为count(distinct)。我很确定SAS支持后者,它是标准的SQL。

答案 1 :(得分:0)

我认为您可以使用ifc语句通过以下技术实现您的需求:

select count(distinct ifc(sex='M',name,'DUMMYVALUE')) -1 as number_of_distinct_male_names
from my sashelp.class

您需要减去-1,因为'DUMMYVALUE'会将1添加到唯一的男性名称列表中。

在您的情况下,由于您要满足2个条件,因此会更复杂一些,因此您需要嵌套的ifc语句:

select count(distinct   
             ifc(if_my_condition1_is_met,
                 unique_value,
                 ifc(if_my_condition2_is_met, 
                     alternate_unique_value, 
                     'DUMMY_VALUE')
                ) -1 as number_of_conditional_unique_values
from my table