标题: 关于用sas添加观测 [打印本页] 作者: shiyiming 时间: 2008-3-8 18:41 标题: 关于用sas添加观测 一个数据集A,包含两个变量a,b
a b
0.1 0.03
0.2 0.04
0.4 0.08
0.5 0.24
0.7 0.51
想得到如下数据集,包含三个变量a,b,c其中a和b还是A中的变量,
只是将以增量为0.1的原则下缺少的a值补充上,另外c=a的平方,对应于新补充的a值的b为缺省.。
a b c
0.1 0.03 0.01
0.2 0.04 0.04
0.3 . 0.09
0.4 0.08 0.16
0.5 0.24 0.25
0.6 . 0.36
0.7 0.51 0.49
请高手帮忙看看这个程序该怎么写?谢谢!作者: shiyiming 时间: 2008-3-9 09:07 标题: Re: 关于用sas添加观测 [code:11k9zi4d]data test1;
input a b;
cards;
0.1 0.03
0.2 0.04
0.4 0.08
0.5 0.24
0.7 0.51
;
run;
data _null_;
set test1 end=last;
if last then call symput('n',a);
run;
data test2;
do _n_=1 to &n*10;
a=_n_/10;
b=.;
c=a**2;
output;
end;
run;
data result;
update test1 test2;
by a;
run;[/code:11k9zi4d]作者: shiyiming 时间: 2008-3-13 19:54 标题: Re: 关于用sas添加观测 [code:1kdznden]
data test;
input a b;
cards;
0.1 0.03
0.2 0.04
0.4 0.08
0.5 0.24
0.7 0.51
;
run;
%let d=0.1;
data result(drop=x y tmp i);
set test1;
x=lag(a);
y=dif(a);
if _n_=1 then do;x=a-&d;y=&d;end;
if y=&d then do;c=a**2;output;end;
else do;
tmp=b;
do i=x+&d to x+y by &d;
a=i;
c=a**2;
if a^=(x+y) then b=.;
else b=tmp;
output;
end;
end;
run;
[/code:1kdznden]