2017-08-15 91 views
-1

跟進到 SAS - transpose multiple variables in rows to columns設置名稱idgroup

我有以下代碼:

data have; 
input CX_ID 1. TYPE $1. COUNT_RATE 1. SUM_RATE 2.; 
datalines; 
1A110 
1B220 
2A120 
    ; 
run; 


    proc summary data = have nway; 
     class cx_id; 
     output out=want (drop = _:) 
     idgroup(out[2] (count_rate sum_rate)= count sum); 
    run; 

所以這個表:

CX_ID TYPE COUNT_RATE SUM_RATE 
    1  A  1  10 
    1  B  2  20 
    2  A  1  20 

成爲

CX_ID COUNT_1 COUNT_2 SUM_1 SUM_2 
1  1  2  10  20 
2  1  .  20  . 

瓦ich是完美的,但我如何設置名稱爲

Count_A Count_B Sum_A Sum_B 

或者一般情況下,無論在有表的類型字段值?

謝謝

+0

如果您將類從CX_ID更改爲您的類型,會發生什麼?或者在CLASS語句中? – Reeza

+0

您發佈的代碼無效。 – Reeza

+0

我需要Cx_ID的最終表格。這個想法是通過Cx_ID按類型在一行中計算產品代碼和費率總和。 – Ben

回答

1

雙PROC TRANSPOSE是動態的,您可以添加數據步驟來輕鬆定製名稱。

*sample data; 

data have; 
    input CX_ID 1. TYPE $1. COUNT 1. SUM 2.; 
    datalines; 
1A110 
1B220 
2A120 
    ; 
run; 

*transpose to long; 

proc transpose data=have out=long; 
    by cx_id type; 
run; 

*transpose to wide; 

proc transpose data=long out=wide; 
    by cx_id; 
    var col1; 
    id _name_ type; 
run;