Reputation: 378
I can't make a sum of all values in a subset of an multidimensional array??
_sum = sum(of _valores[_i,_x,_quadrado,*]);
I know that I can sum one dimension array
E.G. _sum = sum(of _arraName[*]);
Code::
data teste;
array _quadros{3,3,9} _temporary_;
array _valores{3,3,9,9} _temporary_;
/*Zera todos os valores*/
do _i = 1 to 9;
do _z = 1 to 3;
do _t = 1 to 3;
do _x = 1 to 9;
_valores[_z,_t,_i,_x] = 1;
end;
end;
end;
end;
do _quadrado = 1 to 9;
do _i = 1 to 3;
do _x = 1 to 3;
_sum = sum(of _valores[_i,_x,_quadrado,*]);
put _sum =;
end;
end;
end;
run;
Error::
_sum = sum(of _valores[_i,_x,_quadrado,*]);
ERROR 386-185: Expecting an arithmetic expression.
ERROR 200-322: The symbol is not recognized and will be ignored.
ERROR 76-322: Syntax error, statement will be ignored.
Upvotes: 1
Views: 473
Reputation: 51611
Normalize your data instead and use PROC SUMMARY
.
* generate test data ;
data test;
do i = 1 to 9;
do z = 1 to 3;
do t = 1 to 3;
do x = 1 to 9;
valores = 1;
output;
end;
end;
end;
end;
run;
* Sum across last dimension ;
proc summary data=test nway ;
class i z t ;
var valores ;
output out=want sum= ;
run;
Upvotes: 1
Reputation: 12465
I don't think you can do it with the sum function. You can write your own to do it.
proc fcmp outlib=work.fns.fns;
function sumslice4(inArr[*,*,*,*],a,b,c,d) ;
_1 = dim(inArr,1);
_2 = dim(inArr,2);
_3 = dim(inArr,3);
_4 = dim(inArr,4);
_s = 0;
do i=1 to _1;
do j=1 to _2;
do k=1 to _3;
do l=1 to _4;
if (a = i or missing(a)) and
(b = j or missing(b)) and
(c = k or missing(c)) and
(d = l or missing(d)) then
_s = sum(_s,inArr[i,j,k,l]) ;
end;
end;
end;
end;
return (_s);
endsub;
run;
quit;
This creates a function sumslice4()
that takes a 4 dimensional array, and a series of indices. You specify the indices you want summed. Optionally, put a missing, .
, to specify the full index.
Here is a test with your example:
options cmplib=work.fns;
data test;
array _values{3,3,9,9} _temporary_;
do _i = 1 to 9;
do _z = 1 to 3;
do _t = 1 to 3;
do _x = 1 to 9;
_values[_z,_t,_i,_x] = 1;
end;
end;
end;
end;
x = sumslice4(_values,1,1,.,.);
put x=;
x = sumslice4(_values,1,1,1,.);
put x=;
x = sumslice4(_values,1,1,1,1);
put x=;
x = sumslice4(_values,.,1,1,1);
put x=;
run;
I get:
x=81
x=9
x=1
x=3
Which should be what you are expecting.
Upvotes: 2