Page 1 of 1

Sum within a roster with a skip

Posted: March 3rd, 2016, 7:49 am
by Anysia
I have a roster with size 20 with purchases of households items and amount.
the list is applicable to all households on their weekly households expenditure.
want to use row (occurrence) 21 as Total amount spent in a week. TThe following are in the roster
HEID Item Description Quantity
1.
2.
3.
21(Total)
I used the following codes to sum the roster for HHID 6
numeric x6a;
x6a = s5a1 + s5a2 + s5a3 + s5a4 + s5a5

if curocc(SEC5A000) = 6 then
if not $(6)= x6a then
errmsg("Check Total. System calculated is %d and What you keyed is %d",x6a,$(6));
reenter
endif;
endif;
The above codes works well. But my challenge is not all households provided for all HEID (6) items thus a keyer have to key zero (0)
throughout even if the household consume an item for the above checks to work. To resolve this issue i introduced this codes

If S5aID in 1:5 and S5A(S5aID) = " " then
skip to HEID(6);
endif;

After it skips all right but my total check does not work again, rather an error message pop up that NOTApp is expected.
Where should introduce the NOTAPP in my codes

Re: Sum within a roster with a skip

Posted: March 3rd, 2016, 8:00 am
by josh
When you have a skipped (blank) numeric value in CSPro the system assigns it the special value NOTAPPL. When you sum a group of values that includes a NOTAPPL value the sum also becomes NOTAPPL. You should exclude the NOTAPPL values. You can write the logic as follows:
x6a = 0;
if s5a1 <> notappl then
  x6a = x6a + s5a1;
endif;
if s5a2 <> notappl then
  x6a = x6a + s5a2;
endif;
etc...

Re: Sum within a roster with a skip

Posted: March 6th, 2016, 11:58 am
by Saint
In the past, I will define a function like this in proc global:
function av(var)
if var=missing or var=notappl then
av = 0;
else
av = val;
endif;
end;

Then in such functions, you can write x6a=av(s5a1)+av(s5a2)...
In this case, it does not matter if any of the s5a variables is skipped or missing.

Re: Sum within a roster with a skip

Posted: March 12th, 2016, 6:57 am
by Anysia
Please i still have a challenge in summing in my roster, i have then attached my application
for help.
Thanks in advance.

Re: Sum within a roster with a skip

Posted: March 12th, 2016, 7:21 pm
by Saint
Please try the attached and see if it works for you as expected. Best.

Re: Sum within a roster with a skip

Posted: March 16th, 2016, 10:35 am
by Anysia
Thanks so much Saint. A quick one again.
I want to apply same logic in your function but this time around i have 100 roster occurrence, It cumbersome using
the plus (+) for all occurrence.Doing this for 100 rows ...( x5b = av(W1B(1)) + av(W1B(2)) + av(W1B(3)) + av(W1B(4)) + av(W1B(5));
I tried the sum function and am fighting with errors..

I have attached the program and expanded the occurrence to 100.

Thanks in advance.

Re: Sum within a roster with a skip

Posted: March 16th, 2016, 12:29 pm
by Saint
Hi Anysia,
Find attached the revised program that simplifies what you want to do.

Best.