BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chester2018
Obsidian | Level 7

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;

bmi.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;
...

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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;
...
ballardw
Super User

Or

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

 

webart999ARM
Quartz | Level 8

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.

heatherml20
Calcite | Level 5

Hi,  Thank you so much!  I have my next problem running through now accounting for the missing values.

 

:  )  Heather

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3011 views
  • 3 likes
  • 5 in conversation