左连接返回空白

时间:2018-04-04 16:55:54

标签: join sas left-join sas-studio

我已经执行左连接,其中左表有500,000个观察值。在某些情况下,左连接已成功用于Business_Line =“零售”,其中下一个观察结果为空白,为什么会这样?

我使用的代码:

proc sql;
create table joined2 as
select a.*
      ,b.Join1
      ,b.Join2
      ,b.Join3
from joined as a
left join Sasdata.Assumptions as b
on a.Business_Line = b.Business_Line;
quit;

这两个表看起来像

data joined;
input Business_Line $;
datalines;
Retail
Retail
Retail
Business
Business
;
run;

要加入的表格如

data sasdata.assumptions;
input Business_Line $ Join1 Join2 Join3;
datalines;
Retail 10% 10% 10%
Business 20% 10% 5%
;
run;

当前结果表看起来像

 business_line join1 join2 join3
 Retail 10% 10% 10%
 Retail . . .
 Business 20% 10% 5%
 Business . . .

1 个答案:

答案 0 :(得分:2)

示例代码未说明问题。

确实,当实际business_lines值为join1-join3'Retail'时,Business的缺失值不会发生。您将得到一个带有零售的3x1行和带有Business的2x1行的结果。

当左表中的连接键在右表中没有对应的匹配时,会出现缺失值。如果格式化了变量,这可能会在SAS中出现

假设business_line是一个格式化值为

的整数
proc format;
  value line
    101 = 'Retail'
    102 = 'Retail'
    103 = 'Retail'
    201 = 'Business'
    202 = 'Business'
  ;

使用格式化的business_line更新数据

data joined;
input Business_Line;
format Business_Line line.;
datalines;
101
102
102
201
202
run;    

data assumptions;
input Business_Line Join1 Join2 Join3;
format Business_Line line.;
datalines;
101 10 10 10
201 20 10  5
run;

加入具有一些不匹配的基础值

proc sql;
create table joined2 as
select a.*
      ,b.Join1
      ,b.Join2
      ,b.Join3
from joined as a
left join Assumptions as b
on a.Business_Line = b.Business_Line;
quit;

options nocenter; ods listing;
proc print data=joined2;
run;

结果显示缺失值

       Business_
Obs      Line       Join1    Join2    Join3

 1     Retail         10       10       10
 2     Retail          .        .        .
 3     Retail          .        .        .
 4     Business       20       10        5
 5     Business        .        .        .