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!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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