请你能帮助我从中获取数据:
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遇到了一些问题,但我对此并不熟悉。它可能是一个手动的DATA步骤,但我真的很茫然。
谢谢。
答案 0 :(得分: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;
答案 1 :(得分: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;