Header 1 | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | Header 6 | Header 7 | Header 8 | Header 9 | Header 10 | Header 11 | Header 12 | Header 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | cost1 | cost2 | cost3 | cost4 | cost5 | cost6 | cost7 | cost8 | cost9 | cost10 | cost11 | cost12 | end |
1 | . | 0 | 1 | 1 | 4 | 6 | 7 | 2 | 8 | 2 | 8 | 0 | 5 |
2 | 1 | 1 | . | 1 | 2 | 2 | 2 | 2 | 3 | 5 | 7 | 6 | 2 |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 10 |
4 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 12 |
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!
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
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
Thank you! It works!!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.