0
votes

I have a data set with the following structure:

data account;
   input Index c1 c2 c3 c4 c5 c6 ;
   datalines;
4 30 20 10 30 40 20 
3 50 20 30 50 10 20
;
run;

In my file, there are 150+ columns of the "c"-Type containing numbers. In each line, I would like to sum up the c: columns up to the Index variable and put the sum into a new column. In the first line the Index variable is 4, so the result should be sum = c1 + c2 + c3 + c4. In the second line, the Index is 3, so it should be sum = c1 + c2 + c3.

How can I address the columns like this? Do I have to read them into an array first?

1

1 Answers

3
votes

Try this:

data want;
  set account;
  array vars c:;
  sum=0;
  do i=1 to index;
     sum+vars(i);
  end;
  drop i;
  run;