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;
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;
...
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;
...
Or
if 0 lt (bmi_v1) lt 25 then iBMI_v1 = 100;
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.
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 25. 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.