2

I have performed a left join where the left table has 500,000 observations. In some cases the left join has been successful for Business_Line = "Retail" where as the next observation is left blank, why is this?

the code I used:

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;

the two tables look like

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

the table to join looks like

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

the current resulting table looks like

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

1 回答 1

2

示例代码不演示该问题。

实际上,当实际 business_lines 值为或join1-join3时,不会发生缺失值。您会得到 3x1 行的零售结果和 2x1 行的商业结果。'Retail'Business

当左表中的连接键在右表中没有对应的匹配项时,就会出现缺失值。如果变量被格式化,这可能在 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        .        .        .
于 2018-04-04T17:37:38.617 回答