2014年4月28日 星期一

運用 Do loop 整理資料

  • /*
    DO index-variable=start TO stop BY increment;
    SAS statements
    END;
    */
    • data test;
      Amount=1000;
      Rate=.075/12;
      do month=1 to 12; /*開始 Iteration */
          Earned+(amount+earned)*rate;
      end;  /*month 跑到 13 才停,因此最後是 13 */
      run;  /*輸出 所有變項值*/
    • do i=1 to Years;
      變項也可以當成 Stop 的依據
    • /*Decrementing DO Loops ,最後到 1 結束*/
    • a Series of Items
      • DO index-variable=
        2,5,9,13,27;
      • DO index-variable=
        'MON','TUE','WED','THR','FRI';
      • DO index-variable=
        Spring, Summer, Fall , Winter;  變項值

/*Nesting DO Loops*/
  • data work.earn;
    do year=1 to 20; /*每年增資*/
        Capital+2000;
        do month=1 to 12; /*每月複利*/
            Interest=capital*(.012/12);
            capital+interest;
        end;
    end;
    run;

/*Conditionally Executing DO Loops*/
  • /* DO UNTIL(expression); */
    在程式底才判斷,因此statement一定至少執行一次
    • data invest;
          do until (Capital>=9000);
              capital+1000;
              capital+capital*.10;
              Year+1;
              output;
          end;
      run;
      proc print noobs;run;
    • data invest;
          do Year=1 to 10  until (Capital>=20000);
              capital+1000;
              capital+capital*.10;
              output;
          end;
      if year=11 then year=10;
      /*index-variable 跑足就會多 1  */
      run;
      proc print noobs;run;
    • data invest;         /*依 condition 提早結束*/
          do Year=1 to 10  until (Capital>=20000);
              capital+5000;
              capital+capital*.10;
              output;
          end;
          if year=11 then year=10;
      run;
      proc print noobs;run;
  • /* DO WHILE(expression); */
    在程式初就判斷,因此statement可能完全不執行
    • data invest;
          do while (Capital>=9000);
      *不會執行,因為初始是 0 ,要 >= 9000 才會動;
              capital+1000;
              capital+capital*.10;
              Year+1;
              output;
          end;
      run;

系統抽樣 搭配 Point-Output-Stop
  • DATA TT;
        Do sample=1 to 206 by 5 ;
            SET SASUSER.frequentflyers  point=sample;
            Output;
        End;
                    Stop;
    RUN;

沒有留言: