본문 바로가기

SAS/Scenario

Scenario 6

 

 

This scenario uses the Excel file hear.xlsx.

Write a SAS program to do the following and store the results in data set Work.Heart.

 

  • Import the Excel file heart.xslx.
  • Drop the AgeAtDeath and DearthCause variables from the Work.Heart data set.
  • Include only the ovservations where Status=Alive in the Work.Heart data set.
  • If the AgeCHDdiag variable has a missing value(.), then do not include the value in Work.Heart.
  • Create a new variable Smoking_Status, set its length to 17 characters, and use the following criteria:

       - If the value of Smoking is between 0 and less than 6, then Smoking_Status is "None (0-5)".

       - If the value of Smoking is between 6 and 15 inclusively, then Smoking_Status is "Moderate (6-15)"

       - If the value of Smoking is between 16 and 25 inclusively, then Smoking_Status is "Heavy (16-25)"

       - If the value of Smoking is greater than 25, then Smoking_Status to (>25).

  • If there are any other values for the variable Smoking, set Smoking to "Error".
  • Create a two-way frequency table using variables AgeCHDdiag and Smoking_Status and supress column percentage, row percentage, and cell percentage.

 

 

 

[사용 코드]

 

libname cert 'C:/folders/mypath/cert/heart.xlsx' ;       --------→  엑셀파일 로드

data work.heart (drop=AgeAtDeath DeathCause);
set cert.heart;
where Status='Alive';
if AgeCHDdiag = . then delete;
length Smoking_Status $17;
if 0 <= Smoking <6 then Smoking_Status = "None (0-5)";
else if 6 <=  Smoking  <= 15  then Smoking_Status = "Moderate (6-15)";
else if 16 <=  Smoking <= 25  then Smoking_Status = "Heavy (16-25)";
else if Smoking > 25  then Smoking_Status = "Very Heavy (>25)";
else Smoking_Status = "Error" ;
run;


proc freq data=work.heart;
tables AgeCHDdiag * Smoking_Status/norow nocol nopercent;
run;

 

 

 

  • 엑셀파일 불러오는 법 숙지
  • 결측값 나타낼 때 ' . ' 이 아닌 . 으로 표현한다 !!!
  • if - else if - else 순서 기억하기 !!!
  • 길이 LENGTH 문 chracter 값이므로 $17  으로 입력 !
  • 열, 행, 셀 percentage 없애는 것  / norow nocol nopercent 기억하기 !!!
  • PROC FREQ 문에서 table 이 아니라 tables !!

 

  • The LENGTH statement specifies that the length of Smoking_Status is set to 17 and is a character variable.
  • This is used to avoid truncating values.

 

PROC FREQ result

'SAS > Scenario' 카테고리의 다른 글

Scenario 8  (0) 2022.12.25
Scenario 7  (0) 2022.12.25
Scenario 5  (0) 2022.12.25
Scenario 4  (0) 2022.12.25
Scenario 3  (0) 2022.12.25