用PROC SQL转置

时间:2018-06-27 10:07:58

标签: sql sas 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%应该采用这种方法。

2 个答案:

答案 0 :(得分:2)

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

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 

答案 1 :(得分:0)

感谢@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;