2

请你能帮我从中获取我的数据:

Name   + Class   + Teacher
-------+---------+-----------
Fred   + Chem    + Mr Blond
Fred   + Chem    + Mr Pink
Fred   + Maths   + Mr Blond
Barney + Chem    + Mr Brown
Barney + French  + Mr Black
Barney + French  + Mr Blond

对此:

Name   + Class1  + Teacher1_1 + Teacher1_2 + Class2  + Teacher2_1 + Teacher2_2
-------+---------+------------+------------+---------+------------+------------
Fred   + Chem    + Mr Blond   + Mr Pink    + Maths   + Mr Blond   +
Barney + Chem    + Mr Brown   +            + French  + Mr Black   + Mr Blond

所以基本上每个学生一次观察,每个学生的班级调换,每个学生班级的教师调换,但与班级交错。

我不确定这是双 PROC TRANSPOSE 操作还是其他操作。我遇到了一些使用 PROC Summary 的东西,但我对此不太熟悉。这可能是手动数据步骤的事情,但我真的很茫然。

谢谢你。

4

2 回答 2

1

我相信其他人可以提出更优雅的解决方案,但这应该可行....

    proc sort
        data=dataset;
        by name;
    run;

    proc transpose 
        data=dataset
        out=dataset;
        var Class Teacher;
        by Name;
    run;

    DATA teacher_dataset class_dataset;
        set dataset;
        if  _NAME_ = "Teacher"  then  output teacher_dataset;
        else if _NAME_ = "Class"    then  output class_dataset;

    run;


    PROC SQL;
        create table final_dataset as
        select 
                a.Name,
                b.Col1 as Class1,
                a.Col1 as Teacher1,
                b.Col2 as Class2,
                a.Col2 as Teacher2,
                b.Col3 as Class3,
                a.Col3 as Teacher3

        from        teacher_dataset as a
        left join   class_dataset   as b
            on a.Name=b.Name;
    quit;
于 2013-04-10T14:50:06.183 回答
1

非常简单。首先输出为垂直格式,然后转置。与正常的双重转置相比,唯​​一棘手的部分是您的班级/教师组合不是唯一的,因此您必须添加一些额外的逻辑 - 通常一个数组会更好地工作,但在这种情况下不是。

data have;
input Name  $  Class  $  Teacher $;
datalines;
Fred    Chem     MrBlond
Fred    Chem     MrPink
Fred    Maths    MrBlond
Barney  Chem     MrBrown
Barney  French   MrBlack
Barney  French   MrBlond
;;;;
run;

data have_pret;
set have;
array transvars[2] class teacher;
by name class notsorted;
if first.name then counter=0;
if first.class then do;
    class_counter=0;
    counter+1;
    id=cats('Class',counter);
    value=class;
    output;
end;
class_counter+1;
id=cats('Teacher',counter,'_',class_counter);
value=teacher;
output;
run;

proc transpose data=have_pret out=want;
by name notsorted;
id id;
var value;
run;
于 2013-04-10T15:06:03.477 回答