我面临的问题是我有262个库存样本。现在我必须对这些股票配对的每种可能组合进行计算。 我正考虑采取两个循环。但是我编码它的方式,我甚至没有得到任何输出或日志窗口中的错误通知。 首先,我使用proc sql将所有我的股票代码符号放在宏变量中。
proc sql noprint;
select distinct name
into :ticker separated by " "
from stocks;
quit;
%macro close2;
%let n=262; /*262 stocks*/
%do i=1 %to &n.-1;
%do j=i-1 %to &n.;
%let s1=%trim(%scan(&ticker.,&i.," "));
%let s2=%trim(%scan(&ticker.,&j.," "));
data close;
set normp;
closeness=(&s1.-&s2.)**2;
run;
proc means data=close noprint;
var closeness;
output out=close1 (drop=_type_ _freq_)
sum(closeness)=closen;
run;quit;
data=close1;
set close1;
&s1.%s2.=closen;
run;
%end;
%end;
%mend close2;
最后,我希望得到var" closeness"的所有不同总和。以我用它计算的两只股票的两个代码符号命名。
答案 0 :(得分:1)
不要是Loopy(http://www2.sas.com/proceedings/forum2007/183-2007.pdf)
我假设你有一个日期列或其他标识符用于比较。
一些假数据:
%let nStocks=200;
data stocks;
array S[&nStocks];
do date=1 to 10;
do st=1 to &nStocks;
S[st] = rannor(1);
end;
output;
end;
drop st;
run;
将其转换为长而瘦的"而不是"短而宽#34;
proc transpose data=stocks out=stocks_t(rename=(_name_=Stock1 col1=S1_value));
by date;
run;
确保一切都是相同的顺序(在一分钟内很重要)和PROC SQL步骤以获取股票的名称。
proc sort data=stocks_t;
by date Stock1;
run;
proc sql noprint;
select distinct Stock1
into :stocks separated by ' '
from stocks_t
order by Stock1;
quit;
%put &stocks;
现在使用数据步骤查看数据。使用哈希表和循环为每个日期添加不同的组合。计算"亲密度"。
data need;
set stocks_t;
if _n_=1 then do;
%create_hash(lk,date Stock2, S2_Value,"stocks_t(rename=(Stock1=Stock2 S1_Value=S2_value))");
end;
by date;
stocks = "&stocks";
if first.date then
count = 1;
count + 1;
do j=count to &nStocks;
Stock2 = scan(stocks,j);
S2_value = .;
rc = lk.find();
closeness=(S1_Value-S2_Value)**2;
C_Var = catt(Stock1,Stock2);
output;
end;
drop rc count stocks j;
run;
从这里开始,你只需要做好总结就可以了:
proc summary data=need;
var closeness;
class C_Var;
output out=closeness(where=(C_Var^="") drop=_type_ _freq_) sum=;
run;
来自此处的 %create_hash()
宏:https://github.com/FinancialRiskGroup/SASPerformanceAnalytics