使用 PROC SQL 转置

Transpose with PROC SQL

一个最小的例子:

DATA my_data;
INPUT id name $;
DATALINES;
10 Adam
20 Bob
;


DATA my_data2;
INPUT id name $ value year;
DATALINES;
10 Adam 100 2010
10 Adam 200 2017
20 Bob  300 2010
20 Bob  400 2017
;


PROC SQL;
CREATE TABLE TEST AS 
SELECT A.ID, A.NAME,
CASE WHEN B.YEAR = 2010 THEN B.VALUE ELSE . END,
CASE WHEN B.YEAR = 2017 THEN B.VALUE ELSE . END
FROM MY_DATA A 
LEFT JOIN MY_DATA2 B ON A.ID = B.ID AND A.NAME = B.NAME
;
QUIT;

我得到的输出是:

10  Adam  100  .
10  Adam  .    200
20  Bob   300  .
20  Bob   .    400

但我想要的输出是:

10  Adam 100 200
20  Bob  300 400

我知道加入是个问题,但我不是 100% 应该采用的方法。

对 ID 和 NAME 列以及著名的聚合函数使用 GROUPING
( MINMAX ) :

SELECT A.ID, A.NAME,
MIN(CASE WHEN B.YEAR = 2010 THEN B.VALUE ELSE NULL END) VALUE1,
MIN(CASE WHEN B.YEAR = 2017 THEN B.VALUE ELSE NULL END) VALUE2
FROM MY_DATA A 
LEFT JOIN MY_DATA2 B ON A.ID = B.ID AND A.NAME = B.NAME
GROUP BY A.ID, A.NAME
ORDER BY A.ID 

感谢@EzLo。

PROC SQL;
    CREATE TABLE TEST AS 
        SELECT A.ID, A.NAME,
            MAX(CASE WHEN B.YEAR = 2010 THEN B.VALUE ELSE . END),
            MAX(CASE WHEN B.YEAR = 2017 THEN B.VALUE ELSE . END)
        FROM MY_DATA A 
            LEFT JOIN MY_DATA2 B ON A.ID = B.ID AND A.NAME = B.NAME
        GROUP BY A.ID, A.NAME
;
QUIT;