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.
Thanks.
Why should OSA be anything but 1? The condition is met.
Because CAI value for this subject is missing and we do not know what the actual value is. It could be smaller or larger.
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
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);
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.
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 |
Make sure that OSA and CSA are not already present in the dataset, or use an ELSE to set them to missing.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: