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

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
SASKiwi
PROC Star

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;

View solution in original post

6 REPLIES 6
andreas_lds
Jade | Level 19

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

SASKiwi
PROC Star

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;
d6k5d3
Pyrite | Level 9

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

SASKiwi
PROC Star

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

ballardw
Super User

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???

d6k5d3
Pyrite | Level 9

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

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