Help using Base SAS procedures

How to sum selected array variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

How to sum selected array variables

Header 1Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8Header 9Header 10Header 11Header 12Header 13
idcost1cost2cost3cost4cost5cost6cost7cost8cost9cost10cost11cost12end
1.011467282805
211.1222235762
300000000001110
422222222222212

My dataset looks like this. Now, I want to add a new column to get the sum of selected "costXX" variables from 1 to the value of end. For example, I should sum up cost1-cost5 for the first row and cost1-cost12 for the last row. The value of costXX variables could be missing.

I created an array for these variables: array a(12) cost1-cost12. But the sum function can only be used for sum up all of the array variables, right?  tot = sum(of a{*} );


Any one know how to do this?

Thanks!


Accepted Solutions
Solution
‎05-04-2013 06:27 PM
Respected Advisor
Posts: 4,651

Re: How to sum selected array variables

You need an array indeed, but a DO loop as well :

data want;

set have;

array cost{*} cost1-cost12;

do _n_ = 1 to end;

     mySum = sum(mySum, cost{_n_});

     end;

run;

PG

PG

View solution in original post


All Replies
Solution
‎05-04-2013 06:27 PM
Respected Advisor
Posts: 4,651

Re: How to sum selected array variables

You need an array indeed, but a DO loop as well :

data want;

set have;

array cost{*} cost1-cost12;

do _n_ = 1 to end;

     mySum = sum(mySum, cost{_n_});

     end;

run;

PG

PG
Occasional Contributor
Posts: 11

Re: How to sum selected array variables

Thank you! It works!!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 214 views
  • 0 likes
  • 2 in conversation