Answering Calculations

Other discussions about CSPro
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Answering Calculations

Post by MrTaco »

hey josh

I used "if" statement but still it doesnt give me the right calculation when I add "missing"

Thabiso
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Answering Calculations

Post by josh »

You need to explain in English first what you want to happen to your calculation when there is missing. How you handle missing in a calculation depends on the situation. Sometimes you want to treat it as zero, sometimes not. There is not a standard formula. Please explain what you want the result to be in English and then we can help you figure out the code to get there.
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: Answering Calculations

Post by MrTaco »

Hi Josh

I want this code to add up as total... Q2_O = Q2_A + Q2_B but i also need to include "missng" if they didnt answer that question.

Thanks
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Answering Calculations

Post by josh »

I'm assuming that you mean that Q2_A or Q2_B could be missing. The next question is how do you want to interpret missing? There are two ways that I have seen people handle it.

1) Interpret missing as zero. In other words if Q2_A is 10 and Q2B_B is missing then you only count Q2_A and your total is 10.

2) If either Q2_A or Q2_B is missing then the total should also be missing. The idea here is that if one of the variables is not reported you can't be sure what the total really is so you count it as missing as well.

Which one you choose depends on how you want to analyze your data. I will show the code for both.

Assuming you want to count missing as zero then you would do the following (option 1):

Q2_O =
0; // start out with zero

// Add in Q2_A if it is not missing
if Q2_A <> missing then
    Q2_O = Q2_O + Q2_A;
endif;

// Add in Q2_B if it is not missing
if Q2_B <> missing then
    Q2_O = Q2_O + Q2_B;
endif;
If instead you want to make the total missing if either input variable is missing (option 2) then you would do:
if Q2_A = missing or Q2_B = missing then
    
// One of the input variables is missing so make the total missing
    Q2_O = missing;
else
   
// Neither is missing so it is ok to take the sum
    Q2_O = Q2_A + Q2_B;
endif;
Post Reply