loop for sum on arrays

Reply
New Contributor WZ
New Contributor
Posts: 3

loop for sum on arrays

[ Edited ]

I have multiple arrays that I would like to do sum for each of the arrays. Since the variables have similar prefix and surfix, it is very inefficent to do sum one after another...I think  do loop will improve the codes but cannot figure out how to make it work..My codes are like below:

Data Sum;

Set Test;

 

Array A_0(*)  var_1_0  var_2_0   ...  var_n_0;

Array A_1(*)  var_1_1   var_2_1  ...   var_n1_1;

..

Array A_m(*) var_1_m    var_2_m .... var_nm_m;

 

SumA(*)  sumA_1 sumA_2...SumA_m;

Do i = 0 to m;

sumA(i) = sum(of A%&_i(*))  /*this is where I am stuck at*   I am trying to use & to allow value of i change but apparently not working/

end;

run;

 

 

 

 

 

Super User
Posts: 11,134

Re: loop for sum on arrays

It may help to provide a small example of what you are attempting to accomplish. It isn't quite clear if you are trying to sum mulple arrays at one time or trying to sum the same indexed variable in multiple arrays.

You may want a multidimensional array where you have n rows and m columns

 

Or possibly this belongs in Proc IML.

 

Can you provide example input data for 3 rows of 3 varaibles (values should NOT be the same for either column or row) and what the desired output would actually look like.

 

I strongly suspect that bringing macro variables (&_i) is unlikely to help much.

New Contributor WZ
New Contributor
Posts: 3

Re: loop for sum on arrays

[ Edited ]

Thanks for getting back to me..I am monitoring a group variable for multiple people over 8 years. The variables are defined as Var_01_0 for the first person in a group at year 0, and Var_02_0 for the second person in the same group at year 0 and so on. When year changes, Var_01_1 will represent the first person in that group at year 1 and so on . The number of people participated in the experiment, however can change over  years.  Now, I need to sum all persons' status for the same group at each year, which is why Array A_0(*) was created for year 0, so do the other arrays. Obviously when the person and year change, the value of each variable will change. However, the variables all have similar perfix and surfix. I felt it would be more efficient to use a loop to run sum function than do all sum manully...Do I make my question clear?

 

 

Super User
Posts: 5,370

Re: loop for sum on arrays

Do you have control over the names of the variables?  This would be an easy problem if you named the variables VAR_year_person instead of VAR_person_year.

Super User
Posts: 11,134

Re: loop for sum on arrays


WZ wrote:

..Do I make my question clear?

 

 


Not exactly.

Some example data, fake is fine, with a similar structure showing enough variables and records to demonstrate the cases with the desired output will help.

If have some data in a SAS data set Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Trusted Advisor
Posts: 1,479

Re: loop for sum on arrays

[ Edited ]

You need a macro to do the summings. 

 

%macro sum_a(a,n);   /* a=no of arrays. Assuming all arrays are of same length = n */

     do i=1 to &a;

           do j=1 to &n;

               sumA_&i = sum(of sumA_&i , var_&j._&i);     /* array code given &i  is the suffix of variable name */

         end;

     end;

%mend sum_a;

 

data want;

  set have;

       %sum_a(10,30); /* eg. 10 arrays of 30 variables each */

       total = sum(of sumASmiley Happy;  /* total sum of all arrays) */

run;

New Contributor WZ
New Contributor
Posts: 3

Re: loop for sum on arrays

[ Edited ]

Hi, Shmuel...I initially used sumA%&_i but somehow my SAS do not recognize i as an index and cannot run loop and do sum on it. ..Also the arrays in my experiment do  not have equal length

Trusted Advisor
Posts: 1,479

Re: loop for sum on arrays

You probably know, & and % are symbols used with a macro.

The macro code should be defined before using it.

You have not defined the macro (or not show it) therefore SAS didn't recognize it;

 

SAS enables use a short writing like  sum(of varx: ) instead sum(of var01, var02, ... varn).

The sequence number should be the suffix of the variable names.

in your case, the person sequence number is the mid of the variable name.

There fore you need a loop to add each variale separatly.

 

If number of persons are changing per year, you need

- either define the number for each year 

- or add dummy variables with zero or missing value in order to have same number of varables each year.

   Then you can use my posted code.

Super User
Posts: 19,171

Re: loop for sum on arrays

See the options for Variable Lists

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

 

Your arrays have to have consistent number of variables in the data step. The number of rows is irrelevant, given what you've said so far. 

 

Another option - which works better if you have a consistent number of variables per array - is a multi dimensional array. 

Ask a Question
Discussion stats
  • 8 replies
  • 263 views
  • 0 likes
  • 5 in conversation