Sum variables in roster

Discussions about CSEntry
Post Reply
Socio
Posts: 48
Joined: May 1st, 2017, 4:47 am

Sum variables in roster

Post by Socio »

Dear all

I want to sum up variables in roster.

Question
HH146 - links the number of row to roster (HH147)
HH148 - need sum of HH147_6

I'm using command given below:

PROC HH148
preproc
$ = HH147_6(1) + HH147_6(2) + HH147_6(3) + HH147_6(4) + HH147_6(5);


The preoblem here is if I select less than 5 in HH146, It is not giving result.
But if I select 5 in HH146, it shows the sum in HH148.

Please let me know what I miss out. |

Thanks
Attachments
HH146.jpg
HH146.jpg (21.49 KiB) Viewed 3372 times
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Sum variables in roster

Post by josh »

If you have values that are blank in your calculation that could give you bad results. In CSPro arithmetic if any value is blank then the result is blank. If for example HH147_6(5) is blank then and the others are all filled in then when you add them together you will get blank. To avoid this you can check for blanks:
$ = 0;
if HH147_6(1) <> notappl then
  $ = $ + HH147_6(
1);
endif;
if HH147_6(2) <> notappl then
  $ = $ + HH147_6(
2);
endif;
if HH147_6(3) <> notappl then
  $ = $ + HH147_6(
3);
endif;
if HH147_6(4) <> notappl then
  $ = $ + HH147_6(
4);
endif;
if HH147_6(5) <> notappl then
  $ = $ + HH147_6(
5);
endif;
A much simpler solution is to use the sum() function which properly handles blanks:
$ = sum(HH147_6)
Socio
Posts: 48
Joined: May 1st, 2017, 4:47 am

Re: Sum variables in roster

Post by Socio »

Thank you for your fast reply.
I'll use this
$ = sum(HH147_6)
Post Reply