BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NYSAS
Calcite | Level 5

I have a simple question that I can't seem to answer.

I want to create and calculate a new variable IBW using 4 different equations.

Choice of equations depends on 2 other variables 1) sex and 2) heightin (categorized as less than or greater or equal to 60)

When I use the following code, the variable IBW is generated but the calculation is not performed (value is missing for all observations) without an error message.

data b;
	set a;
	heightin = height/ 2.54;
	IBW = .;
	if sex = 'F' and heightin lt 60 then IBW = (45 - (60- (heightin*2.3)));
	if sex = 'F' and heightin ge 60 then IBW = (45 + ((heightin-60)*2.3));
	if sex = 'M' and heightin lt 60 then IBW = (50 - (60- (heightin*2.3)));
	if sex = 'M' and heightin ge 60 then IBW = (50 + ((heightin-60)*2.3));
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
bobpep212
Quartz | Level 8

I got the following to work without revising the code you provided at all. My guess...you have an issue with the input sex variable - either case sensitivity or length. Try strip(upcase(sex))='F' instead of just sex='F'.

 

data a;
input sex $1. height;
datalines;
M 135
F 145
;
run;

data b;
	set a;
	heightin = height/ 2.54;
	IBW = .;
	if sex = 'F' and heightin lt 60 then IBW = (45 - (60- (heightin*2.3)));
	if sex = 'F' and heightin ge 60 then IBW = (45 + ((heightin-60)*2.3));
	if sex = 'M' and heightin lt 60 then IBW = (50 - (60- (heightin*2.3)));
	if sex = 'M' and heightin ge 60 then IBW = (50 + ((heightin-60)*2.3));
run;

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

If IBW is always missing then for each record (1) height is missing and/or (2) sex is not 'M' or 'F'.  If height is missing, then the statement "heightin=height/2.54;" would generate an error message on your log, and result in missing value for heightin, consequently IBW.   But if sex in not 'M' or 'F' then there would be no error message, but missing values for IBW.

 

So what does you log say?  If no error message then height is never missing.  In that case you should examine the values of SEX.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

Why not just restructure your logic to make it easier to determine?

data b;
  set a;
  heightin = height/ 2.54;
  IBW = .;
  if  sex='F' then do;
    if missing(heightin) then put 'Missing heightin' ;
    else if heightin lt 60 then IBW = (45 - (60- (heightin*2.3)));
    else IBW = (45 + ((heightin-60)*2.3));
  end;
  else if sex='M' then do;
    if missing(heightin) then put 'Missing heightin';
    else if heightin lt 60 then IBW = (50 - (60- (heightin*2.3)));
    else IBW = (50 + ((heightin-60)*2.3));
  end;
  else put 'Unknown SEX' sex=;
run;

 

bobpep212
Quartz | Level 8

I got the following to work without revising the code you provided at all. My guess...you have an issue with the input sex variable - either case sensitivity or length. Try strip(upcase(sex))='F' instead of just sex='F'.

 

data a;
input sex $1. height;
datalines;
M 135
F 145
;
run;

data b;
	set a;
	heightin = height/ 2.54;
	IBW = .;
	if sex = 'F' and heightin lt 60 then IBW = (45 - (60- (heightin*2.3)));
	if sex = 'F' and heightin ge 60 then IBW = (45 + ((heightin-60)*2.3));
	if sex = 'M' and heightin lt 60 then IBW = (50 - (60- (heightin*2.3)));
	if sex = 'M' and heightin ge 60 then IBW = (50 + ((heightin-60)*2.3));
run;
NYSAS
Calcite | Level 5

Thanks to all for your ideas. Ultimately it was a case sensitivity issue.

NYSAS
Calcite | Level 5
Great, thank you! This worked perfectly. (Beginner mistake)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1046 views
  • 1 like
  • 4 in conversation