預處理的幾個步驟需要....
裝入數據:
data a1 ;
input a b c ;
cards ;
2 3 4
1 2 3
;run ;
data a2 ;
input a b d ;
cards ;
0 0.3 1
0 0.2 0
;run ;
拉在A1和A2的數據集的所有變量名(更新您的LIBNAME如果需要):
proc sql ;
create table data1 as
select libname, memname, name, label
from sashelp.vcolumn
where libname= 'WORK' and memname in ('A1','A2')
order by name
;quit ;
只保留這兩者共同的數據集的變量:
data data2 ;
set data1 ;
by name ;
if last.name and not first.name ;
run ;
把這兩樣列表和公共變量的數量成宏變量:
proc sql ;
select name
into :commvarnames separated by ' '
from data2
;
select count(name)
into :commoncount
from data2
;quit ;
閱讀源數據集 - 加載第一,將它們轉移到一個臨時數組(因此他們不會覆蓋變量值),然後加載第二個數據集並在do循環中進行計算:
data output ;
set a1(keep=&commvarnames) ;
array one(&commoncount) _temporary_ ;
array two(&commoncount) &commvarnames ;
* Load A1 to temporary array ;
do i=1 to &commoncount ;
one(i)=two(i) ;
end ;
* Load A2 to variables ;
set a2(keep=&commvarnames) ;
do i=1 to &commoncount ;
product=sum(product,one(i)*two(i)) ;
end ;
run ;
是的,這似乎有點複雜,然後我想。我應該嘗試使用一些預處理來避免這種計算。 – Lovnlust