BookmarkSubscribeRSS Feed
fdehkord
Obsidian | Level 7

I am creating two variables using if, then statement.

In the statement OAI> CAI, it returns a value of 1 for the variable OSA since 18.7 is greater than the missing value. OSA should be missing not 1. How do I correct this?

Here is the two lines of data and the codes that I have used.

Screenshot 2022-07-20 154544.jpg

Thanks.

9 REPLIES 9
fdehkord
Obsidian | Level 7

Because CAI value for this subject is missing and we do not know what the actual value is. It could be smaller or larger.

Reeza
Super User

SAS defines missing as the lowest value, if you want to ignore missing values you need to code that logic into your IF statement explicitly. Use the NOT MISSING() function or NMISS() to check multiple variables at once.

 

if nmiss(CAI, OAI, AHI) = 0 and ...rest of your criteria
ballardw
Super User

Please post code as text, preferably in a text or code box opened using the </> or "running man" icons that appear above the message window.

 

If you only want to assign values to variables with the independent variables are not missing it is your responsibility to test for that before any calculation.

Missing values are less than any actual value so the condition is true.

You can test a number of numeric values for missing using the NMISS function:

 

If nmiss(AHI, CAI, OAI) > 0 then <do whatever you do when one of these is missing>;

Else if AHI >= 15 ...

 

Since you have multiple statements that would depend on that likely

If nmiss(AHI, CAI, OAI) = 0 then do;
   if AHI >= ... OSA=0;
   if AHI >= ... CSA=0;
end;

Note that SAS will return a numeric 1 for True and 0 for False for logical comparisons. So instead of an if/then/else you could use:

   OSA = (AHI>=15 and OAI>CAI);

 

mkeintz
PROC Star

As you realize, for numeric variables, the missing value is considered "less than" all valid numeric values.  And the ">" operator (unlike arithmetic operators) will not object when one or both values are missing.  Perhaps you want something like

 

  if nmiss(oai,cai)=0 then OSA=(OAI> CAI);

which will leave OSA as missing if either OSI or CAI is missing.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
fdehkord
Obsidian | Level 7

Thank you, but I still have the same problem. Here are the codes that I used and the output.

Data HFPEE;
Set myHFPEE;
If nmiss(AHI,CAI, OAI) = 0 then OSA = (AHI>=15 and OAI>CAI) ;
If nmiss(AHI,CAI, OAI) = 0 then CSA= (AHI>=15 and CAI>=OAI);
Run;

Obs

AHI

CAI

OAI

OSA

CSA

43

39.4

.

18.7

1

0

393

23

2.5

.

0

1

Reeza
Super User
data have;
infile cards;
input ahi cai oai;
cards;
39.4 . 18.7
23 2.5 .
;;;;
run;

Data HFPEE;
Set have;
If nmiss(AHI,CAI, OAI) = 0 then OSA = (AHI>=15 and OAI>CAI) ;
If nmiss(AHI,CAI, OAI) = 0 then CSA= (AHI>=15 and CAI>=OAI);
Run;

proc print data=HFPEE;run;

Guessing that you already have OSA/CSA in your data set then as the above works as expected and need to set them to missing before the calculation. 

fdehkord
Obsidian | Level 7

 OSA/CSA were present in my data set from the previous runs and as soon as I remove them it worked fine. Thank you very much.

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!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 751 views
  • 8 likes
  • 5 in conversation