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.
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;
Why do you compare with 9999 when you want to check for missing values?
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;
@SASKiwi, Thank you so much. I was such a stupid!
@d6k5d3 - It's easy to get this type of logic mixed up. I'm still doing it after over 30 years of using SAS
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???
@ballardw, Awesome! Thank you so much for the insight.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.