现在有一个data,
data x;
input x $ y $ z;
cards;
a 1 2
a 2 2
a 3 3
a 4 1
a 6 2
b 2 4
b 3 3
b 4 5
b 6 1
c 3 0
c 6 1
;
其中y相当于星期几,像A的只有一二三四六,缺少星期五以及星期天,现在假定缺少的都看作是0.
怎样才能构建一个新的数据,把它变成以下的形式呢
data y;
input x $ y $ z;
cards;
a 1 2
a 2 2
a 3 3
a 4 1
a 5 0
a 6 2
a 7 0
b 1 0
b 2 4
b 3 3
b 4 5
b 5 0
b 6 1
b 7 0
c 1 0
c 2 0
c 3 0
c 4 0
c 5 0
c 6 1
c 7 0
;
run;
[code:2gg9s09g]proc sort nodupkey; by x y; run;
data _temp;
set x(keep = x); by x;
length y $8.;
if first. x then do _n_ = 1 to 7;
y = put(_n_, F1.); z = 0;
output;
end;
run;
data have;
update _temp x; by x y;
run;[/code:2gg9s09g]
[code:3repb8qw]data raw;
input x $ y z;
cards;
a 1 2
a 2 2
a 3 3
a 4 1
a 6 2
b 2 4
b 3 3
b 4 5
b 6 1
c 3 0
c 6 1
;
data out(drop=y rename=(start=y));
set raw;
by x;
if first.x then start=1;
do while(start lt y);
z=0;
output;
start+1;
end;
set raw point=_n_;
output;
start+1;
if last.x then
do while(start le 7);
z=0;
output;
start+1;
end;
run;[/code:3repb8qw]
好啊。每次我仔细阅读楼上哥们的程序,我都受益匪浅。
[code:3danto1y]
data out1;
set raw; by x notsorted;
array zz{7} _temporary_;
if first.x then do _n_ = 1 to 7;
zz[_n_] = 0;
end;
zz[y] = z;
if last. x then do y =1 to 7;
z = zz[y];
output;
end;
run;[/code:3danto1y]