左连接和合并多个变量

时间:2017-03-28 02:41:34

标签: sql sas

我有3个数据集:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.5.1/snap.svg-min.js"></script>
</head>
<body>
  
  <div style="width: 335px; height: 600px;">
  
    
    <svg width='100%' height='100%' id='container'></svg>
    
  </div>
    
    </body>
</html>

Sales1,Sales2和Sales3与B和C的变量相同。

我的目标是将联接数据集B和C留给A以获取所需的销售信息sales1,sales2和sales3。

但是,我只知道如何进行左连接并与1变量合并。

  A containing variables period, region, city

  B containing variables period, city, sales1, sales2, sales3

  C containing variables period, region, sales1, sales2, sales3

  The sample data sets are:
  A:
  Period    City   Region
  1          A      North
  2          B      South
  3          C      East

  B:
  Period    City    Sales1    Sales2    Sales3
   1         A        12        13        14
   2         B         1         2         3

  C:
  Period    Region   Sales1    Sales2    Sales3
   1         North     6        7         8
   2         South     3        2         5

我的问题是,如果有下一个变量sales2和sales3的快速方法吗?

1 个答案:

答案 0 :(得分:0)

您的查询可简化为:

proc sql;

     create table merged1 as
     select A.*,
     coalesce (b.sales1, c.sales1) as sales1,
     coalesce (b.sales2, c.sales2) as sales2,
     coalesce (b.sales3, c.sales3) as sales3

from A
     left join B on A.period=B.period and A.city=B.city
     left join C on A.period= C.period and A.region=C.region;
quit;

输出:

 Period=1 City=A Region=North sales1=12 sales2=13 sales3=14 
 Period=2 City=B Region=South sales1=1 sales2=2 sales3=3 
 Period=3 City=C Region=East sales1=. sales2=. sales3=.

另一种选择是使用proc transpose转置所有表格,因此销售额将位于行中,而城市/地区位于列中。

使用的数据:

data A;
input
Period    City $   Region $ ;
datalines;
  1          A      North
  2          B      South
  3          C      East
;
run;
data B;
input
  Period    City  $  Sales1    Sales2    Sales3 ;
  datalines;
   1         A        12        13        14
   2         B         1         2         3
;
run;
data C;
input
  Period    Region $  Sales1    Sales2    Sales3 ;
  datalines;
   1         North     6        7         8
   2         South     3        2         5
   ;
run;