重塑数据集长到宽

时间:2014-08-07 15:59:00

标签: sas

我以为我明白了如何使用proc transpose,但我似乎在这里遗漏了一些东西:

有:

fam pay quarter
1   100 7
2   20  7
3   34  7
2   54  8
3   64  8
1   36  9

想要:

fam qtr_7   qtr_8   qtr_9
1   100       0      36
2   20       54       0
3   34       64       0

代码:

proc transpose data=have out=want prefix=qtr_;
by fam;
id quarter
var pay;
run;

我不断收到错误消息“ID值_7被观察两次”

作为一种后门方式,我尝试了:

data want;
    set have;
        if quarter = 1 then qtr1 = pay;
            else if quarter = 2 then qtr2 = pay;
            else if quarter = 3 then qtr3 = pay;
            else if quarter = 4 then qtr4 = pay;
            else if quarter = 5 then qtr5 = pay;
            else if quarter = 6 then qtr6 = pay;
            else if quarter = 7 then qtr7 = pay;
            else if quarter = 8 then qtr8 = pay;
            else if quarter = 9 then qtr9 = pay;
            else if quarter = 10 then qtr10 = pay;
            else if quarter = 11 then qtr11 = pay;
run;

但是,我仍然需要折叠行,我无法弄清楚该怎么做。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先,要使用by语句,您需要对数据集进行排序。

proc sort data=have;
by fam quarter;
run;

然后尝试转置代码。