/* BMI는 meter로 된 키, kg으로 된 몸무게도 정의된다. 그런데 이용할 sashelp.heart에 있는 데이터는 단위가 다르다.
1. height(inch), weight(pound)을 각각 meter, kg으로 변환한다.
2. meter, kg으로 변환된 값들로 BMI를 계산한다.
3. BMI로 underweight, normal, overweight, obesity로 분류하여 BMIC라고 정의한다.
bmi <= 18.5 underweight
18.5 < bmi <= 25 normal
25 < bmi <= 30 overweight
bmi > 30 obesity
변환된 height_m, weight_kg는 새로 생성되었으므로,
bmi를 계산할때 "calculated"라는 keyword를 붙여줘야한다.
if~ then 은 SQL에서 case when then으로 정의한다. case when은 "end"로 끝나야한다.missing여부는 "is null" / "is not null"로 표현한다. data step에서 하듯이 연속형인 경우 "=." 이산형인 경우 =''도 된다. 그러나 is null / is not null 이 SQL 표준이고, 이 표현은 이산형/연속형을 구분할 필요가없다.
le는 <=, ge는 >=, lt는 <, gt는 >
proc sql;
select status, sex, height, weight,
height*0.0254 as height_m,
weight*0.453592 as weight_kg,
calculated weight_kg / (calculated height_m)**2 as bmi,
case when calculated bmi le 18.5 & calculated bmi is not null then 'Underweight'
when calculated bmi gt 18.5 & calculated bmi lt 25 then 'Normal weight'
when calculated bmi ge 25 & calculated bmi lt 30 then 'Overweight'
when calculated bmi ge 30 then 'Obesity'
end as bmic
from sashelp.heart
;quit;
/** 확인해본후 데이터의 저장은 "create table 테이블이름 as"를 proc sql 밑에 추가하면 된다.
아래와 같이 하면 bmi라는 sas dataset이 만들어진다.
**/
proc sql;
create table bmi as
select status, sex, height, weight,
height*0.0254 as height_m,
weight*0.453592 as weight_kg,
calculated weight_kg / (calculated height_m)**2 as bmi,
case when calculated bmi le 18.5 & calculated bmi is not null then 'Underweight'
when calculated bmi gt 18.5 & calculated bmi lt 25 then 'Normal weight'
when calculated bmi ge 25 & calculated bmi lt 30 then 'Overweight'
when calculated bmi ge 30 then 'Obesity'
end as bmic
from sashelp.heart
;quit;
/**
연습: sashelp.class에 있는 19명 학생의 bmi와 bmi상태에 따른 4가지 분류를 나타내는 변수를 같은 방식으로 만들어보아라.
**/