将名称设置为idgroup

时间:2017-08-15 16:33:53

标签: sas

跟进 SAS - transpose multiple variables in rows to columns

我有以下代码:

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      .

哪个是完美的,但如何将名称设置为

Count_A Count_B Sum_A Sum_B 

或者一般来说,无论表格的类型字段中的值是什么?

谢谢

1 个答案:

答案 0 :(得分: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;