在此我就不过多介绍proc transpose的具体使用方法,只是在此简单的举个实例,让大家了解这两种实现数据转置的方法即可,希望各位大牛们批评指正,能够做些补充。
/*此数据集为学生各科考试的成绩记录,希望一个学生姓名后面几列跟的是学生的各个科目的成绩*/
data quan1;
input name$ course $ score;
cards;
zhangsan a 59
zhangsan b 86
zhangsan c 75
zhangsan d 76
lisi a 78
lisi c 85
lisi e 79
run;
/*先对原始数据集进行排序*/
proc sort data=quan1;
by name;
run;
/*1、transpose转置实现 把纵向数据集按照需要 转换成横向数据集*/
proc transpose data=quan1 out=tt(drop=_name_);
by name;
id course;
var score;
run;
/*data步转置实现 :把纵向数据集按照需要 转换成横向数据集*/
data temp;
set quan1;
array amount(5) a b c d e;
retain a b c d e;
by name;
if first.name then
do i=1 to 5;
amount(i)=0;
end;
if course="a" then
amount(1)=score;
else if course="b" then
amount(2)=score;
else if course="c" then
amount(3)=score;
else if course="d" then
amount(4)=score;
else if course="e" then
amount(5)=score;
if last.name;
drop i score course;
run;
/*2、transpose步转置实现 把横向数据集按照需要 转换成纵向数据集*/
proc transpose data=temp out=dsd(rename=(_name_=course col1=score) );
by name;
run;
/*data步转置实现 把横向数据集按照需要 转换成纵向数据集*/
data temp1;
set temp;
array aa(5) a b c d e;
retain a b c d e;
by name;
do i=1 to 5;
if aa(i) ne 0 and aa(i) ne . then do;
if i=1 then course="a";
else if i=2 then course="b";
else if i=3 then course="c";
else if i=4 then course="d";
else if i=5 then course="e";
score=aa(i);
output;
end;
end;
drop i a b c d e;
run;
这样横向纵向转置就可以初步实现,如果还想做些其他细节的修改,需要再添加一些选项即可



雷达卡







[victory][victory]
京公网安备 11010802022788号







