BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
s100
Calcite | Level 5
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!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

2 REPLIES 2
PGStats
Opal | Level 21

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
s100
Calcite | Level 5

Thank you! It works!!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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