BookmarkSubscribeRSS Feed
ErikT
Obsidian | Level 7

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)?

4 REPLIES 4
art297
Opal | Level 21

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

Ksharp
Super User

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

ErikT
Obsidian | Level 7

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{*});

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 924 views
  • 0 likes
  • 3 in conversation