用一个笨办法,将date设为num,用滞后,效率不高,也许合乎要求:
data a;
input name $ date price;
if name="a" then date2=lag(date);
else if name="b" then date2=lag(date);
else;
cards;
a 20080101 23
a 20080102 43
a 20080103 134
a 20080104 43
a 20080105 243
a 20080106 43
a 20080107 32
a 20080108 4
b 20080101 324
b 20080102 3
b 20080103 43
b 20080104 23
b 20080105 3
b 20080106 32
;
run;
再来一下(前提是日期连续):
data a;
input name $ date price;
pr=price;
datelag=lag(date);
if date-datelag ne 1 then pr=.;
else;
cards;
a 20080101 23
a 20080102 43
a 20080103 134
a 20080104 43
a 20080105 243
a 20080106 43
a 20080107 32
a 20080108 4
b 20080101 324
b 20080102 3
b 20080103 43
b 20080104 23
b 20080105 3
b 20080106 32
;
run;
proc print;
run;
1 a 20080101 23 . .
2 a 20080102 43 43 20080101
3 a 20080103 134 134 20080102
4 a 20080104 43 43 20080103
5 a 20080105 243 243 20080104
6 a 20080106 43 43 20080105
7 a 20080107 32 32 20080106
8 a 20080108 4 4 20080107
9 b 20080101 324 . 20080108
10 b 20080102 3 3 20080101
11 b 20080103 43 43 20080102
12 b 20080104 23 23 20080103
13 b 20080105 3 3 20080104
14 b 20080106 32 32 20080105
try this:
/*实现price1的上移*/
proc expand data=a out=aa(drop=time) method=none;
by name;
convert price1=price/transformout=(lead 1);
run;
/*实现因子的计算*/
data result;
do _n_=1 by 1 until(last.name);
set aa;
by name;
if _n_=1 then yinzi=price/price1;
else yinzi=(price/price1)*yinzi;
output;
end;
run;