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;
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;
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.
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;
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;
Thanks to all for your ideas. Ultimately it was a case sensitivity issue.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.