Hi, I am calculating BMI from a patient's Height and Weight per patient per date. For each BMI calculation I would like to keep Patient, VisitDate, Visit, and bflag information. There is a typo for patient B on Jan12019 where weight is not marked with the same Visit and bflag info that Height is. I would like for the BMI to have that info. Is there a way I can keep the visit and bflag?
data in;
input Patient $ VisitDate $ Value Unit $ Type $ Visit $ bflag $;
cards;
A Jan12019 1 m Height baseline Y
A Jan12019 50 kg Weight baseline Y
A Jan52019 2 m Height visit1 .
A Jan52019 55 kg Weight visit1 .
B Jan12019 1 m Height baseline Y
B Jan12019 50 kg Weight . .
B Jan52019 55 kg Weight baseline Y
;
proc sort data=in;
by patient VisitDate;
run;
data have;
set in;
by patient VisitDate;
retain height_;
if first.VisitDate then height_=.;
if type='Height' then height_=Value;
if last.VisitDate then bmi=value/height_**2;
output;
if bmi ne . then do;
type='BMI';
value=bmi;
unit='kg/m2';
output;
end;
drop height_ bmi;
run;
data want;
input Patient $ VisitDate $ Value Unit $ Type $ Visit $ bflag $;
cards;
A Jan12019 1 m Height baseline Y
A Jan12019 50 kg Weight baseline Y
A Jan12019 50 kg/m2 BMI baseline Y
A Jan52019 2 m Height visit1 .
A Jan52019 55 kg Weight visit1 .
A Jan52019 13.75 kg/m2 BMI visit1 .
B Jan12019 1 m Height baseline Y
B Jan12019 50 kg Weight . .
B Jan12019 50 kg/m2 BMI baseline Y
B Jan52019 55 kg Weight baseline Y
;
data in;
input Patient $ adtm $ Value Unit $ Type $ avisit $ bflag $;
cards;
A Jan12019 1 m Height baseline Y
A Jan12019 50 kg Weight baseline Y
A Jan52019 2 m Height visit1 .
A Jan52019 55 kg Weight visit1 .
B Jan12019 1 m Height baseline Y
B Jan12019 50 kg Weight . .
B Jan52019 55 kg Weight baseline Y
;
proc sort data=in;
by patient adtm descending Type;
run;
data want;
set in;
by patient adtm descending Type;
retain weight_ height_;
if first.adtm then do;
weight_=.;
height_=.;
end;
if type='Weight' then
weight_=Value;
else if type='Height' then
height_=Value;
if last.adtm then do;
bmi=weight_/height_**2;
end;
output;
if bmi ne . then
do;
type='BMI';
value=bmi;
unit='kg/m2';
output;
end;
drop height_ weight_ bmi;
run;
Hi Reeza, I believe this is a systemic issue since I have seen other patients with this issue. Please let me know if you have any further questions. Thanks!
Not exactly what you want, but close maybe a start?
data in;
input Patient $ VisitDate $ Value Unit $ Type $ Visit $ bflag $;
cards;
A Jan12019 1 m Height baseline Y
A Jan12019 50 kg Weight baseline Y
A Jan52019 2 m Height visit1 .
A Jan52019 55 kg Weight visit1 .
B Jan12019 1 m Height baseline Y
B Jan12019 50 kg Weight . .
B Jan52019 55 kg Weight baseline Y
;
proc sort data=in;
by patient descending VisitDate;
run;
data want;
set in;
by patient descending VisitDate;
retain height_ height_;
if first.VisitDate then do;
height_=.;
weight_=.;
end;
if type='Height' then
height_=Value;
else if type='Weight' then
weight_=Value;
if last.VisitDate then do;
if weight_ = . then weight_ = lag(value);
if Visit = . then Visit = lag(Visit);
if bflag = . then bflag = lag(bflag);
bmi=weight_/height_**2;
end;
output;
if bmi ne . then
do;
type='BMI';
value=bmi;
unit='kg/m2';
output;
end;
drop height_ weight_ bmi;
run;
proc sort data=want;
by patient VisitDate;
run;
data in;
input Patient $ adtm $ Value Unit $ Type $ avisit $ bflag $;
cards;
A Jan12019 1 m Height baseline Y
A Jan12019 50 kg Weight baseline Y
A Jan52019 2 m Height visit1 .
A Jan52019 55 kg Weight visit1 .
B Jan12019 1 m Height baseline Y
B Jan12019 50 kg Weight . .
B Jan52019 55 kg Weight baseline Y
;
proc sort data=in;
by patient adtm descending Type;
run;
data want;
set in;
by patient adtm descending Type;
retain weight_ height_;
if first.adtm then do;
weight_=.;
height_=.;
end;
if type='Weight' then
weight_=Value;
else if type='Height' then
height_=Value;
if last.adtm then do;
bmi=weight_/height_**2;
end;
output;
if bmi ne . then
do;
type='BMI';
value=bmi;
unit='kg/m2';
output;
end;
drop height_ weight_ bmi;
run;
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.
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.