DATA Step, Macro, Functions and more

Array arguments in function calls

Reply
Contributor
Posts: 31

Array arguments in function calls

If you want to to calculate for instance the mean value of the arguments of an array you can specify that as:

ARRAY MyArray {6,4};

Mean_Value = MEAN(OF MyArray{*});

This will calculate the mean value over the complete array, so for the two dimensions together.

I could not find a similar construct to calculate the mean value over just one dimension. For instance let us assume that we want the mean value of the 6 rows of the second column.

I tried something like Mean_Value_2 = MEAN(OF MyArray{*,2}); but that does not work.

Has anybody an idea how to specify this (other than explicitly specify all individual elements)?

PROC Star
Posts: 7,471

Re: Array arguments in function calls

Erik,  As long as you know the actual locations within the array you could always use something like:

    Mean_Value = MEAN(OF MyArray3-MyArray4);

That would work for any any-dimensional array.

e.g., take a look at: Using multidimensional arrays

Super User
Posts: 10,023

Re: Array arguments in function calls

ErikT,  I think the arrary you are talking about is temporary Arrary not normal variable Arrary .

For your example :

data _null_;
array a{6,4} _temporary_ ( 2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5) ;
 do i=1 to dim1(a) ;
  sum+a{i,2};
 end;
 mean=sum/dim1(a) ;
 put sum= mean= ;
run;

Or if these data is stored in a dataset , that would be more simple by using SQL.

Ksharp

Contributor
Posts: 31

Re: Array arguments in function calls

Thanks Art and Ksharp, but...

The idea was to avoid the use of any deterministic programming. The actual situation in my program is that the array is not 6x4, but 26x8 and that in the course of the data step I have to go through all 8 columns one at the time and that repeatedly..

So what I will do is copy the right column to a separate one-dimensional array and use the * notation on that array. I don't think copying the array is a very elegant solution, but for the time being I think it is the best, until somebody comes with a better solution.

do n=1 to dim(MyArray,1);

   temparray{n} = MyArray{n,column};

end;

mean_column = mean(of MyArray{*});

Super User
Posts: 10,023

Re: Array arguments in function calls

ErikT , I think you can do it better . Is you using temporary array , not variable array ?

data _null_;
array a{6,4} _temporary_ ( 2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5
                           2 3 4 5) ;
do j=1 to dim2(a);
 sum=0;
 do i=1 to dim1(a) ;
  sum+a{i,j};
 end;
 mean=sum/dim1(a) ;
 put 'Column'+(-1) j '   ' sum= mean= /;
end;
run;

Ksharp

Ask a Question
Discussion stats
  • 4 replies
  • 311 views
  • 0 likes
  • 3 in conversation