Fluorite | Level 6

## If/Then/Missing Statements

Hi!

I'm having trouble with my If/Then/Missing statements. For this example, I'm making BMI categories, but my new variable keeps making my missing values into one of the categories. I'm trying to get if bmi_v1 > 25, then iBMI_v1 = 100 and so on. There are missing values for bmi_v1, but the iBMI puts the missing values as "100" instead of ".".

``````data patient.guidelines;
set  patient.health;
if (bmi_v1) lt 25 then iBMI_v1 = 100;
else if (bmi_v1) lt 30 then iBMI_v1 = 70;
else if (bmi_v1) lt 35 then iBMI_v1 = 30;
else if (bmi_v1) lt 40 then iBMI_v1 = 15;
else if (bmi_v1) ge 40 then iBMI_v1 = 0;
else iBMI_v1 = .;
run;``````

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: If/Then/Missing Statements

Because that is what you told it to do.  Comparison operators, like < , treat missing values as less than any actual number.

``````if missing(bmi_v1) then iBMI_v1 = .;
else if (bmi_v1) lt 25 then iBMI_v1 = 100;
...``````
3 REPLIES 3
Super User

## Re: If/Then/Missing Statements

Because that is what you told it to do.  Comparison operators, like < , treat missing values as less than any actual number.

``````if missing(bmi_v1) then iBMI_v1 = .;
else if (bmi_v1) lt 25 then iBMI_v1 = 100;
...``````
Super User

## Re: If/Then/Missing Statements

Or

``if 0 lt (bmi_v1) lt 25 then iBMI_v1 = 100;``

Quartz | Level 8

## Re: If/Then/Missing Statements

One way to fix this problem is to include an additional condition to check for missing values in the `if` statement. In SAS, missing values are represented by a dot (`.`), so you can add a condition to check if the value of `bmi_v1` is equal to `.` and assign the missing value to `iBMI_v1` in that case.

Here is an example of how you can modify your code to handle missing values:

``````data patient.guidelines;
set  patient.health;
if bmi_v1 = . then iBMI_v1 = .;
else if (bmi_v1) lt 25 then iBMI_v1 = 100;
else if (bmi_v1) lt 30 then iBMI_v1 = 70;
else if (bmi_v1) lt 35 then iBMI_v1 = 30;
else if (bmi_v1) lt 40 then iBMI_v1 = 15;
else if (bmi_v1) ge 40 then iBMI_v1 = 0;
run;``````

In the code above, the first `if` statement checks if the value of `bmi_v1` is equal to `.` and assigns the missing value to `iBMI_v1` if that is the case. This ensures that any missing values in `bmi_v1` will be preserved in `iBMI_v1` and not assigned to one of the categories.

Discussion stats
• 3 replies
• 1322 views
• 3 likes
• 4 in conversation