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

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

3 REPLIES 3
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.

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
  • 3 replies
  • 1370 views
  • 3 likes
  • 4 in conversation