11
SAS 實實實 2012/11/22 實實實 [email protected]

2012-11-22 sas

Embed Size (px)

DESCRIPTION

我是鄭凱駿 這是我今天教課的

Citation preview

Page 1: 2012-11-22 sas

SAS 實習課2012/11/22

鄭凱駿[email protected]

Page 2: 2012-11-22 sas

Array 的用法• 目的:當要輸入的資料間具有規則性時,

可藉由使用 Array 的方式來簡化程式碼的繁雜 <= 因為 Array 可將資料作暫時的順序性儲存

• Array 在輸入時常會搭配 do end 指令運用• Array 在輸入時也常會搭配 if…then 的判斷

指令運用

Page 3: 2012-11-22 sas

Array function• Purpose:

when data has ordinary processed order,it will be useful to use array to act as a temporary storage base and this could largely reduce the complexity of your code.

data;input etc.celsius_temp1 = 5/9(temp1 – 32);celsius_temp2 = 5/9(temp2 – 32);. . .celsius_temp24 = 5/9(temp24 – 32);run;

data;input etc.array temperature_array {24} temp1-temp24;array celsius_array {24} celsius_temp1-celsius_temp24;do i = 1 to 24;celsius_array{i} = 5/9(temperature_array{i} – 32);end;run;

27 lines

Only 9 lines

Page 4: 2012-11-22 sas

Example

• 如果我們要將 24 個溫度變數從華氏改成攝氏,最直覺的程式寫法是一個變數一個變數慢慢地改

data;input etc.celsius_temp1 = 5/9(temp1 – 32);celsius_temp2 = 5/9(temp2 – 32);. . .celsius_temp24 = 5/9(temp24 – 32);run;

data;input etc.array temperature_array {24} temp1-temp24;array celsius_array {24} celsius_temp1-celsius_temp24;do i = 1 to 24;celsius_array{i} = 5/9(temperature_array{i} – 32);end;run;

總共要 27 行

總共只要 9 行

24 行

Page 5: 2012-11-22 sas

練習• 假設我們現在有 10 個人身高與體重的資料,已

知BMI=體重 <kg>/( 身高 <m>)^2

身高 體重1.5 50

1.6 60

1.7 70

1.6 60

1.7 70

1.8 80

1.5 55

1.6 65

1.7 75

1.7 73

BMI

?

Page 6: 2012-11-22 sas

Exercise

• How to transfer 10 people’s weight and height into BMI index身高 體重1.5 50

1.6 60

1.7 70

1.6 60

1.7 70

1.8 80

1.5 55

1.6 65

1.7 75

1.7 73

BMI

?

Page 7: 2012-11-22 sas

• data bmi_calculate;• array cal(10);• do i=1 to 10;do end 的用法必定從 do 指令起頭 (do end function need to start with do)• Input h w @@;• cal(i)=w/(h*h);• bmi=cal(i);• output;• end; 在 do 指令使用完後一定要以 end 結尾• cards; (do end function need to end with end)• 1.5 50• 1.6 60• 1.7 70• 1.6 60• 1.7 70• 1.8 80• 1.5 55• 1.6 65• 1.7 75• 1.7 73• ;• Proc print data=bmi_calculate;• Var h w bmi;• run;

如果要一行一行打的話 就要手動 keyin 10 行bmi 的數值

Page 8: 2012-11-22 sas

練習• 假設我們要進一步將不同 BMI 值的資料進行分

類,例如 BMI>24 代表體重過重; BMI<=24代表正常

?Type

Page 9: 2012-11-22 sas

Exercise• How to transfer BMI index into another

criteria based nominal index?

?Type

Page 10: 2012-11-22 sas

data bmi_calculate;array cal(10);array cri(10)$;type=' ';do i=1 to 10;Input h w @@;cal(i)=w/(h*h);bmi=cal(i);if bmi>24 then cri(i)='fat';else cri(i)='normal';type=cri(i);output;end;cards;1.5 501.6 601.7 701.6 601.7 701.8 801.5 551.6 651.7 751.7 73;Proc print data=bmi_calculate;Var h w bmi type;run;

可用加入錢號的方式使 array內可輸入文字

定義 type 為一文字變數 , 並多空幾格讓後面分類的文字能完整呈現

Page 11: 2012-11-22 sas

Any questions?