🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pyrite | Level 9

Where am I wrong in this conditional calculation?

I want to calculate Surprise_A = (Act - SurvA)/ STD. But there are missing values in each of Act, SurvA and STD. And, STD can also be 0. If there is a missing value in any of these, or STD = 0, I would not calculate Surprise_A for that observation. So I coded the following way:

``````if Act NE 9999 or SurvA NE 9999 or STD NE 9999 or STD NE 0 then
Surprise_A=(Act-SurvA)/STD;
else Surprise_A=9999;``````

I am not getting desired results. I still get note for division by 0, and I don't see any Surprise_A having  9999.

Where am I wrong?

I much appreciate your time as always.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

Re: Where am I wrong in this conditional calculation?

Try this:

``````if Act NE 9999 and SurvA NE 9999 and STD not in (0, 9999) then Surprise_A=(Act-SurvA)/STD;
else Surprise_A=9999;``````
6 REPLIES 6
Jade | Level 19

Re: Where am I wrong in this conditional calculation?

Why do you compare with 9999 when you want to check for missing values?

PROC Star

Re: Where am I wrong in this conditional calculation?

Try this:

``````if Act NE 9999 and SurvA NE 9999 and STD not in (0, 9999) then Surprise_A=(Act-SurvA)/STD;
else Surprise_A=9999;``````
Pyrite | Level 9

Re: Where am I wrong in this conditional calculation?

@SASKiwi, Thank you so much. I was such a stupid!

PROC Star

Re: Where am I wrong in this conditional calculation?

@d6k5d3 - It's easy to get this type of logic mixed up. I'm still doing it after over 30 years of using SAS

Super User

Re: Where am I wrong in this conditional calculation?

Just because a data source uses 9999 or similar code for missing does not mean that you have to perpetuate that in a SAS data set. Since SAS has an actual "missing" value (actually many more with special missing like .A .B etc) .

So set codes that mean missing to be missing for SAS variables.

Then instead of lots of NE 9999 you can use the MISSING function to test individual variables or NMISS for multiple

Assuming you had actual SAS missing

If nmiss(ACT, SurvA, STD) =0 and STD ne 0 then …

nmiss returns the number of missing values for numeric variables. So if any of them are missing the result would be greater than 0.

Imagine writing a similar condition for 15 or more variables.

Use of SAS missing values means that you wouldn't even need the Else. If the value isn't calculated then SAS automatically sets it to missing.

Now suppose at some later time you want to calculate a mean (or max or standard deviation or practically any statistic except min) for the Surprise_A variable (or Act or SurvA or STD). Do you really want to include the values of 9999 in the calculation???

Pyrite | Level 9

Re: Where am I wrong in this conditional calculation?

@ballardw, Awesome! Thank you so much for the insight.

Discussion stats
• 6 replies
• 877 views
• 3 likes
• 4 in conversation