有一组SAT考试的成绩数据分别是name math verbal
Jackson 570 620 Smith 430 510 Smith 520 .
Baker 620 650 Smith 530 570 Jones 550 640
Williams 560 690 Jackson 600 680 Gray 560 540
White 520 580 White 520 580 Adams 640 730
现在要求是计算total,同时用array 来把每个人的math verbal total 列出来。
结果是这个样子的。
name math1 math2 math 3 verbal1 verbal2 verba3 total1 total2 total3
jackson
smith
baker
jones
williams
hite
adams
因为有的人考了三次,有的人没有,所以把没有数据的用. 表示就可以了,
input name $ math verbal @@;
total=math+verbal;
proc sort data=SAT;
by name;
run;
proc print data=SAT;
run;
先把数据数近来,结果是
name math verbal total
而且name里有的名字重复2-3次,怎样用array把这个名字变成只出现一次,结果如上面列出的那样的?谢谢
data a;
input name $ math verbal @@;
cards;
Jackson 570 620 Smith 430 510 Smith 520 .
Baker 620 650 Smith 530 570 Jones 550 640
Williams 560 690 Jackson 600 680 Gray 560 540
White 520 580 White 520 580 Adams 640 730
;
run;
proc sort data=a;
by name;
run;
data b c d;
set a;
by name;
if first.name then output b;
if last.name=1 and first.name^=1 then output c;
if first.name^=1 and last.name^=1 then output d;
run;
data e;
merge b(rename=(math=math1 verbal=verbal1))
c(rename=(math=math2 verbal=verbal2))
d(rename=(math=math3 verbal=verbal3));
by name;
total1=sum(math1,verbal1);
total2=sum(math2,verbal2);
total3=sum(math3,verbal3);
run;
可以这样做:
data sat;
input name $ math verbal @@;
datalines;
Jackson 570 620 Smith 430 510 Smith 520 .
Baker 620 650 Smith 530 570 Jones 550 640
Williams 560 690 Jackson 600 680 Gray 560 540
White 520 580 White 520 580 Adams 640 730
;
run;
proc sort data=sat;
by name;
run;
data sat;
set sat;
retain num 1;
by name;
if first.name then num=1;
else num+1;
run;
data satt;
set sat;
by name;
keep name math1-math3 verbal1-verbal3 total1-total3;
retain math1-math3 verbal1-verbal3;
array mathv{3} math1-math3;
array verbalv{3} verbal1-verbal3;
array totalv{3} total1-total3;
if first.name then
do i = 1 to 3;
mathv{i} = .;
verbalv{i} = .;
end;
mathv{num} =math;
verbalv{num} =verbal;
do num=1 to 3;
totalv{num}=sum(mathv{num},verbalv{num});
end;
if last.name;
run;
研究一下新手练手系列
[code:00bca]
data a;
input name $ math verbal @@;
cards;
Jackson 570 620 Smith 430 510 Smith 520 .
Baker 620 650 Smith 530 570 Jones 550 640
Williams 560 690 Jackson 600 680 Gray 560 540
White 520 580 White 520 580 Adams 640 730
;
proc sort data=a;
by name;
run;
data e;
set a;
by name;
retain math1-math3;
retain verbal1-verbal3;
retain total1-total3;
array kk(3) math1-math3;
array mm(3) verbal1-verbal3;
array total(3) total1-total3;
if first.name then i=1;
kk(i)=math;
mm(i)=verbal;
total(i)=kk(i)+mm(i);
i+1;
if last.name then output;
drop i;
proc print data=e;
run;
[/code:00bca]
多谢提醒,代码修改如下
[code:89ad0]data a;
input name $ math verbal @@;
cards;
Jackson 570 620 Smith 430 510 Smith 520 .
Baker 620 650 Smith 530 570 Jones 550 640
Williams 560 690 Jackson 600 680 Gray 560 540
White 520 580 White 520 580 Adams 640 730
;
proc sort data=a;
by name;
run;
data e;
set a;
by name;
retain math1-math3;
retain verbal1-verbal3;
retain total1-total3;
array kk(3) math1-math3;
array mm(3) verbal1-verbal3;
array total(3) total1-total3;
if first.name then i=1;
kk(i)=math;
mm(i)=verbal;
total(i)=sum(kk(i),mm(i));
i+1;
if last.name then do;output;
do i=1 to 3; kk(i)=.; mm(i)=. ;total(i)=.;
end; end;
drop i math verbal;
proc print data=e;
run;[/code:89ad0]