Sum dataframe Columns with numeric suffix based on whether the suffix is greater than values of anther column

24 Views Asked by At

Apologies in advance if my description is not clear enough. Basically looking to add up columns in a dataframe with numeric suffix like a1, a2,a3 but would like the summation to be based on values in another column labelled say Indicator with values like 1,2,3. For example if column Indicator is equal to 2 then I will like to add up columns a2 and a3 since the suffix of the column names is >= column Indicator value of 2.

Tried to do this in SAS but did not work

1

There are 1 best solutions below

0
Tom On

This is a straight forward application of an ARRAY. First define the array with all of the variables. The use the extra variable as the starting point for the DO loop to do the summation.

data want; 
  set have;
  array x a1-a100;
  do index=Indicator to dim(x);
     total=sum(total,x[index]);
  end;
  drop index;
run;