|
data test;
input stkcd year month date return;
datalines;
1 2000 1 1 0.01
1 2000 1 2 0.02
1 2000 1 3 0.01
2 2000 1 1 -0.01
2 2000 1 2 0
2 2000 1 3 0.01
3 2000 1 1 -0.02
3 2000 1 2 -0.01
3 2000 1 3 0
;
data result(keep = year month date stkcd return comp_stkcd comp_return result);
retain year month date stkcd return comp_stkcd comp_return result;
if _N_= 1 then do;
declare hash h(dataset: "test", ordered: 'no');
declare hiter iter('h');
h.defineKey('year', 'month', 'date', 'stkcd');
h.defineData('stkcd', 'return', 'year', 'month', 'date');
call missing(stkcd, return, year, month, date);
h.defineDone();
end;
set test(rename = (stkcd = comp_stkcd return = comp_return year = comp_year month = comp_month date = comp_date));
/* Iterate over the hash */
rc = iter.first();
do while (rc = 0);
if (year = comp_year and month = comp_month and date = comp_date and stkcd ne comp_stkcd) then do;
result = (return - comp_return)**2;
output;
end;
rc = iter.next();
end;
run;
这样计算出两两股票在同一天的差的平方
|