SlideShare une entreprise Scribd logo
1  sur  11
SAS 實習課
 2012/11/22

       鄭凱駿
icefu4k6@gmail.com
Array的用法
• 目的:當要輸入的資料間具有規則性時,
  可藉由使用Array的方式來簡化程式碼的繁
  雜<=因為Array可將資料作暫時的順序性儲
  存
• Array在輸入時常會搭配do end 指令運用
• Array在輸入時也常會搭配if…then的判斷指
  令運用
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
                                 data;      Only 9 lines
complexity of your code.         input etc.
  data;      27 lines                array temperature_array {24}
  input etc.                         temp1-temp24;
  celsius_temp1 = 5/9(temp1 –        array celsius_array {24}
  32);                               celsius_temp1-
  celsius_temp2 = 5/9(temp2 –        celsius_temp24;
  32);                               do i = 1 to 24;
  ...                                celsius_array{i} =
  celsius_temp24 =                   5/9(temperature_array{i} –
  5/9(temp24 – 32);                  32);
  run;                               end;
                                     run;
Example
              data;      總共要27行
• 如果我們要將 24   input etc.
              celsius_temp1 = 5/9(temp1 – 32);
  個溫度變數從華     celsius_temp2 = 5/9(temp2 – 32);
                                                           24行
              ...
  氏改成攝氏,最     celsius_temp24 = 5/9(temp24 – 32);
              run;
  直覺的程式寫法
  是一個變數一個     data;
                         總共只要9行

  變數慢慢地改      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;
練習
• 假設我們現在有10個人身高與體重的資料,已知
  BMI=體重<kg>/(身高<m>)^2
                 BMI
 身高    體重




            ?
 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
Exercise
• How to transfer 10 people’s weight and height
  into BMI index                  BMI
   身高     體重




                      ?
   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
•   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                                                    手動keyin 10行
•   1.6 60                                                    bmi的數值
•   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;
練習
• 假設我們要進一步將不同BMI值的資料進行分類,
  例如BMI>24代表體重過重;BMI<=24代表正常

                  Type




           ?
Exercise
• How to transfer BMI index into another
  criteria based nominal index?

                                    Type




                      ?
data bmi_calculate;
array cal(10);          可用加入錢號的方式使array
array cri(10)$;        內可輸入文字
type=' ';            定義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 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 type;
run;
Any questions?

2012-11-22 sas

  • 1. SAS 實習課 2012/11/22 鄭凱駿 icefu4k6@gmail.com
  • 2. Array的用法 • 目的:當要輸入的資料間具有規則性時, 可藉由使用Array的方式來簡化程式碼的繁 雜<=因為Array可將資料作暫時的順序性儲 存 • Array在輸入時常會搭配do end 指令運用 • Array在輸入時也常會搭配if…then的判斷指 令運用
  • 3. 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 data; Only 9 lines complexity of your code. input etc. data; 27 lines array temperature_array {24} input etc. temp1-temp24; celsius_temp1 = 5/9(temp1 – array celsius_array {24} 32); celsius_temp1- celsius_temp2 = 5/9(temp2 – celsius_temp24; 32); do i = 1 to 24; ... celsius_array{i} = celsius_temp24 = 5/9(temperature_array{i} – 5/9(temp24 – 32); 32); run; end; run;
  • 4. Example data; 總共要27行 • 如果我們要將 24 input etc. celsius_temp1 = 5/9(temp1 – 32); 個溫度變數從華 celsius_temp2 = 5/9(temp2 – 32); 24行 ... 氏改成攝氏,最 celsius_temp24 = 5/9(temp24 – 32); run; 直覺的程式寫法 是一個變數一個 data; 總共只要9行 變數慢慢地改 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;
  • 5. 練習 • 假設我們現在有10個人身高與體重的資料,已知 BMI=體重<kg>/(身高<m>)^2 BMI 身高 體重 ? 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
  • 6. Exercise • How to transfer 10 people’s weight and height into BMI index BMI 身高 體重 ? 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
  • 7. 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 手動keyin 10行 • 1.6 60 bmi的數值 • 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;
  • 8. 練習 • 假設我們要進一步將不同BMI值的資料進行分類, 例如BMI>24代表體重過重;BMI<=24代表正常 Type ?
  • 9. Exercise • How to transfer BMI index into another criteria based nominal index? Type ?
  • 10. data bmi_calculate; array cal(10); 可用加入錢號的方式使array array cri(10)$; 內可輸入文字 type=' '; 定義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 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 type; run;