2014年4月24日 星期四

建構變項

累積加總
  • data stress; set sasuser.stress2;
    TotalTime=(timemin*60)+timesec;
    SumSec+totaltime;
    run;
    • SumSec 在 + 的左邊,它的起啟值就是 0 ,而非 missing
    • 其結果類似以下右欄
      1  1
      1  2
      1  3
      1  4

RETAIN variable <initial;-value>;
  • 主要特色
    • data complie 的指令,無法作用於 set, merge的變項
    • 指定變項初始值為任何文字或數字,避免變項在每次iteration被重置
  • SumSec 由10000 起算而非 0
    retain SumSec 10000;
    sumsec+totaltime;

IF-THEN/ELSE 的指令,會比 很多的 IF-THEN 更省資源,或者改用 SELECT-WHEN 的語法
  • SELECT (select-expression);
    WHEN ( condition,…, condition ) Execute-statement;
    WHEN ( condition,…, condition ) Execute-statement;
    OTHERWISE <Execute-statement>;
    END;
    • select (TimeMin) ;
          when ( '13' ) TimeGroup='Lower';
      雖然TimeMine是數值,也一定要 “”
    • OTHERWISE  一要有,否則有
      ERROR: Unsatisfied WHEN clause


變項特性的設定
  • The length of a new variable is determined by the first reference in the DATA step。若要用LENGTH 來指定變項長度,應放在最前端的程式。
    • length Test $ 10;
  • LABEL var= ‘Description’;
  • FORMAT var formats;

DROP and KEEP 變項
  • 用在 Data 、set 、Proc data= 的 Option
    • (DROP=variable(s))
      (KEEP=variable(s))
      • data work (drop=age group);
      • set clinic    (keep=age group BMI );
      • 讀檔時 (SET) 只進 age group BMI ,新資檔(DATA) 刪除 age group,最後輸出只有BMI
  • 在 DATA step 的statement
    • DROP variable(s) ;
      KEEP variable(s);

沒有留言: