具有多个ID值的Proc转置。

时间:2014-07-04 14:28:52

标签: sas transpose proc

您好我有如下数据,当我编写代码时,它会给出如下错误。, 我尝试了不同的方法,但不确定我是否做得对。,

regid   Analysis    value
1378979 Samsjuklighet_L 0
1378979 andrainsatser_2_L   0
1378979 behandling_1_L  5
1378979 behandling_4_L  1
1378979 behandlingsinsatser_2a_L    6
1378979 behandlingsmål_1_L  1
1378979 behandlingsmål_1_L  2
1378979 behandlingsmål_1_L  3
1378979 behandlingsmål_1_L  4
1378979 behandlingsmål_1_L  5
1378979 behandlingsmål_1_L  6
1378979 boende_1_L  1
1378979 boende_2_L  1
1378979 droger_2xc_L    2

我写了如下代码

proc transpose data=X
out=Y;
var value;
by regid;
id Analysis;
run;

但是它给出了错误

ERROR: The ID value "behandlingsm_l_1_L" occurs twice in the same BY group.
ERROR: The ID value "behandlingsm_l_1_L" occurs twice in the same BY group.
ERROR: The ID value "behandlingsm_l_1_L" occurs twice in the same BY group.
ERROR: The ID value "behandlingsm_l_1_L" occurs twice in the same BY group.
ERROR: The ID value "behandlingsm_l_1_L" occurs twice in the same BY group.

我也走了一条新路。

data X; 
set X  ;by  regid analysis value; 
if first.analysis then n = 0; 
n+1; 
run;

proc sort data = X; 
by  regid analysis value n; 
run;

proc transpose data=X out=temp1 (drop=n) let  ;
by regid;
var value;
id analysis;
run;

这会在上面的错误中发出警告,但会删除所有重复项。但我希望获得变量的所有多个值。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

正如sas日志所述,您需要在每个组中按唯一值变换值。这不是真的因为regid总是具有相同的价值。要拥有所有重复项,您可以创建一个变量,以便analysis的值是唯一的。

data input;
infile datalines dlm=" ";
informat Analysis $25.;
input regid   Analysis  $  value;
datalines;
1378979 Samsjuklighet_L 0
1378979 andrainsatser_2_L   0
1378979 behandling_1_L  5
1378979 behandling_4_L  1
1378979 behandlingsinsatser_2a_L    6
1378979 behandlingsmål_1_L  1
1378979 behandlingsmål_1_L  2
1378979 behandlingsmål_1_L  3
1378979 behandlingsmål_1_L  4
1378979 behandlingsmål_1_L  5
1378979 behandlingsmål_1_L  6
1378979 boende_1_L  1
1378979 boende_2_L  1
1378979 droger_2xc_L    2
;
run;
proc sort data=input;
    by regid Analysis;
run;
data input;
    set input;
    retain conta;
    by regid Analysis;
    if first.Analysis then conta=0;
    conta+1;
run;
proc sort data=input;
    by regid conta;
run;
proc transpose data=input out=output(drop=_NAME_ conta);
    var value;
    by regid conta;
    id Analysis;
run;

要小心,因为如果analysis在任何情况下都超过32个字符,那么它会给你一个错误