我的数据集types
为100 rows x 61 columns
。第一列表示Type_ID
,其余60列表示时间段。
在另一个数据步骤calculate
;我想创建一个多维数组timeType{100,60}
,它由来自types
的100行和60列数据初始化。
到目前为止我读过的所有SAS文档都使用显式列引用初始化多维数组,然后数组强制列适合数组维度。
如何有效地将数据集读入数组?
答案 0 :(得分:2)
与昆汀非常相似。我使用Dorfman制作了着名的构造do _n_ = 1 by 1 until(end)
作为阅读循环。
data types;
do type_id = 1 to 100;
array x x1-x60;
do over x; global_counter+1; x = global_counter; end;
output;
end;
run;
data calculate(keep=testid result) typesread_check(keep=x1-x60);
array matrix(100,60) _temporary_;
* load matrix;
do _n_ = 1 by 1 until (end);
set types end=end;
array x x1-x60; * this array is composed of variables that are filled by the SET operation;
do _i_ = 1 to dim(x);
matrix(_n_,_i_) = x(_i_);
end;
end;
* unload matrix to check load with COMPARE;
* can be commented out / removed after confidence established;
do _n_ = 1 to 100;
do _i_ = 1 to 60;
x(_i_) = matrix(_n_,_i_);
end;
output typesread_check;
end;
* perform computations that output;
testid = 1;
result = sum(of matrix(*));
output calculate;
run;
* output is zero rows of differences. That means the matrix was populated correctly;
proc compare noprint data=types(drop=type_id) compare=typesread_check out=diff outnoequal;
run;
如果要沿着类型数据保留计算结果,请删除_temporary_
选项。 keep (testid result matrix:)
获取具有6,000个对应于矩阵(100,60)
答案 1 :(得分:1)
我在阵列上并不大,而且下面没有赢得任何风格点,但这只是一个开始。
基本方法是使用DoW循环将所有数据读入二维数组。
data have;
do TypeID=1 to 5;
p1=10*TypeID;
p2=100*TypeID;
p3=1000*TypeID;
output;
end;
run;
data _null_;
*DOW loop to read data into array;
do until (last);
set have end=last;
array timeType(5,3) _temporary_;
array p{*} p:;
row++1;
do col=1 to dim(p);
timeType{row,col}=p{col};
end;
end;
*PUT the values of the array to the log, for checking;
do i=1 to dim1(timeType);
do j=1 to dim2(timeType);
put timeType{i,j}=;
end;
end;
drop row col i j;
run;
答案 2 :(得分:0)
改为使用HASH对象。首先将您的费率转换为普通数据集而不是#34;矩阵"。或者只是以这种方式创建它。
data rates;
do type=1 to 3 ;
do time=1 to 3 ;
input rate @@ ;
output;
end;
end;
cards;
.1 .2 .3 .4 .5 .6 .7 .8 .9
;
现在让我们制作一些示例数据。我包含了一条与费率表中任何记录都不匹配的记录。
data have ;
input type time amount expected;
cards;
1 2 100 20
2 3 100 60
4 5 100 .
;
现在将费率加载到HASH对象中,并使用.FIND()方法找到当前TYPE和TIME组合的费率。
data want ;
set have ;
if _N_ = 1 then do;
declare hash h(dataset:"rates");
rc = h.defineKey('type','time');
rc = h.defineData('rate');
rc = h.defineDone();
call missing(rate);
drop rc;
end;
if (0=h.find()) then payment=amount*rate;
run;
结果。
Obs type time amount expected payment rate
1 1 2 100 20 20 0.2
2 2 3 100 60 60 0.6
3 4 5 100 . . .