2014年4月20日 星期日

Reading Raw Data File

指定外部資料檔的位置,與讀檔
  • SAS系統檔,Libname
    libname NHIS 'c:\report';
  • 其他檔案,Filename
    • 單檔 filename nhis "z:\DATA\admit.txt";
      nhis 代表實體檔案
      • DATA work.admit;
            infile nhis delimiter='09'x MISSOVER DSD  firstobs=2;
            input ....;
      • filename nhis clear;
    • 多檔 Filename NHIS2 'c:\report\';
      • DATA work.admit;
            infile nhis2(admit.txt) delimiter='09'x MISSOVER DSD  firstobs=2;
            input...;
    • 直接呼叫檔案
      • DATA work.admit;
            infile "z:\DATA\admit.txt" delimiter='09'x MISSOVER DSD  firstobs=2;

Column Input
  • INPUT variable <$> startcol-endcol . . .;
    適用於 Fixed-field Records
    • 條件:固定的n個變項,變項為標準的數字與文字,原始資料要按位子對齊好
    • 指定變項的起點與終點,因此,變項間的順序就不重要了。資料欄位也可被重覆讀取。
  • 用 Infile 
    • libname libref 'SAS-data-library';
      filename nhis "z:\DATA\admit.txt";
    • DATA work.admit;
    • infile "z:\DATA\admit.txt" obs=10 ;
    • input ID 1-10 Name $ 20-30 Sex 15;
    • run;
  • 用 Datalines (Reading Instream Data )
    • DATA work.admit;
    • input ID 1-10 Name $ 20-35 Sex $ 15;
    • DATALINES;
    • 2458          F    Murray, W
    • 2462          M    Almers, C
    • ;
    • 不必再 run
  • Datalines 換行讀取  (僅讀入部分變項)
    • data coat;
    • input category high1-high3 / low1-low3;
    • datalines;
    • 5555 9 8 7 6
    • 4 3 2 1
    • 8888 21 12 34 64
    • 13 14 15 16
    • ;
  • DATA ERROR訊息,不會中斷DATA STEP。

Formatted Input
  • 適用於 Fixed-field Records standard and nonstandard data
    @ n 與 + n  可以交互使用
  • INPUT @n Var-name Informat. ;
    n表示第幾個欄位(position)起
    • input @15 ID $5. @1 LastName $10. @30 Payment 8.
  • INPUT +n Var-name Informat.;
    用+n 要加到變項的起點,要注要讀完一個變項後,column pointer 會自動往後一位,LastName 在第10位End,到第15位的ID時,要 +4 就可以了。
    往回讀資料,要用  + (-n)
    • input LastName $10. +4 ID $5.  +10 Payment 8.
  • Informat 告知 SAS 原始資料的儲存方式,才能順利讀取檔案,但是在 PDV裡,變項不自動以 informat指定格式儲存。
    • PERCENTw.d      w為總長,d為小數位數
    • COMMAw.d
      • 適用在數值卻有  , $ %   等符號 
    • date7.         date9.
      mmddyy8.   mmddyy10.
      datetime.
      TIME8. 

Record Formats
Raw data 儲存observation的方式
  • Fixed-Length Records
    每筆的 end of record marker 都在同一位置 
  • Variable-Length Records
    不固定的 end of record marker
    • 有fixed-field data的,某區段都固定給某變項,例如欄位長固定( 4.)
      PAD option
      • 有些筆數的值較短(ex: 10,還有兩個位元),會提早遇到end of record,避免電腦傻傻執意要換到下列補足長度(另2位元)
      • 本欄為 missing

Free-Format Data
欄位的 begin 與 end 沒有完全固定position
  • List Input與Delimiter(DLM)
    raw data file的限制:數字與文字都只能 8位元,超過會 truncated;標準變項;以空白或符號分隔變項;文字或數字裡不能有該符號;missing要用 . 或其他文字表示。
    • Infile "z:\DATA\admit.txt" DLM=’符號’
      Input
      var1 var2
      var3-var6 
      (var7-var10) ($)   ….. varN ;
  • 解決方案
    • MISSOVER option:某列尾端有Missing value
    • DSD option:某列開頭、列中有missing value
      • 預設Delimiter 為 , 
      • ,, 表示 missing
      • 值的引號”會被移除
    • Length statement:文字大於8位元
      • Infile ….; Length Var $20. ; input Id Var …..;
        注意此時Input裡的 Var 不必再加 $ 了。但因為先以 Length設定,Var是第一個變項,而非 Id
    • & 文字值含有空白(不連續),如 Taipei city
      • 其後的Delimiter 要改為兩個空白「  」
      • Length City $ 12; Input …  city & ;
        或 Input … city & $12. ;
    • : 數值或文字長度超過8,且不內含空白
      • 文字時,需設 Length
      • 數值時,不需設 Length,用 Comma. 即可
        • input name: $13.  pop: Comma.  ;
          datalines;
          TaipeiCity 100,000,000
          KeelungCity 100000000
          ;

讀 Excel 檔
  • libname libref 'location-of-Excel-workbook' <options>;
    • libname readxls "Z:\DATA\finance.xls";
    • proc contents data=readxls._all_ ; run; *可直接知道sheet細詳資訊;
    • data finance ;  set   READXLS."'1$'"n  ;run;
    • libname readxls clear; *停止連結 xls 檔;
  • 可用的 option 
    • DBMAX_TEXT=n
      單欄文字長度
    • GETNAMES=YES|NO
      第一列為變項名
    • MIXED=YES|NO
      所有資料轉成文字,預設是no
    • SCANTEXT=YES|NO
      以最長的列寬為預設列寬。
    • SCANTIME=YES|NO
      no 只有time 的變項 也用 date9.
      yes 改用 time8.
    • USEDATE=YES|NO
      yes date/time 的變項 用 date9.no  改用 datetime.

標準的與非標準的變項
  • 數值型
    • 標準的: 羅馬數字、小數數字、正負號數字、科學記號(ex: E-notation)
    • 非標準的:內含(%$,)、日期時間、分數、二進位、十六進位

建構變項的OPERATOR
  • arithmetic operator,依優先順序
    • -(負號)、**(指數)
    • 乘除
    • 加減
  • Comparison Operators
    • > < = ^= >= <=
    • in ( 1 ,2 ,3) 或  in ( 1 2 3) 
    • in ( "LINK" , "Value" )
  • Logical Operators
    • AND & ,  OR  | ,  NOT ^ 
      • 用 OR的注意事項
        • if TimeMin<12 or 14; 
          • OR 前後要分開看, 2 一定是 true,本條件形同虛設
        • if TimeMin<12 or TimeMin=14;
          • 會輸出 <12 還有 =14 的。

新增日期、時間變項 (待續補充)
  • 日期(兩位年從1920起至2019)
    •                   Date='01jan2014'd;              Date='01jan14'd;
    • 以上可選  attrib date format=date9.; 或 attrib date format=date7.;
    • 或            attrib date format=mmddyy10.;   attrib date format=mmddyy8.;
                      01/01/2014                                 01/01/14
  • 時間 
    • Time='21:00't;
    • attrib TIME format=TIME8.;  21時0分0秒
  • Datetime
    • DateTime='01jan2014:21:00:00'dt;
    • attrib DateTime format=datetime.;

輸出Raw Data File
  • 把sas dataset 轉成 依 column 對齊的 txt檔
    • data _null_;
    • set sasuser.admit;
    • file "z:\admit_column.txt"; *沒有 file 就 put 到 log裡;
    • Put
    •   ID  4.
    •   @6 Name  15.    /* 用 @ 位置  VAR  長度  比較不會出錯  */
    •   @22 Sex  1.
    • run;
  • Sas dataset 轉成 Free-Format raw data;
    • PUT variable <: format>;
      • file …;  Put ID  Fee: 7.2 ;
    • DLM=’’
      • file … DLM=’,’;
    • DSD
      • file … DSD;
      • 預設Delimiter 為 ,
      • 有 , 的數值,會包在一組雙引號中
    • PROC EXPORT DATA=SAS-data-set;
      OUTFILE=filename <DELIMITER='delimiter'>;
      RUN;
  • 存到 xls 檔的 sheet
    • libname readxls "Z:\DATA\finance.xls";
    • data readxls.admit;
    • set sasuser.admit; *不必用 'admit$'n ;
    • run;

運用filename, PIPE, infile 與 windows語法互動, 參考Back Up with Each Submit and Save Your Sanity
    • filename backup pipe 'dir C:\backup /t:w /a:-d /OD';
    • data backup;
    •  infile backup missover pad length=len;
    •  input @01 line $varying200. len; 
    • run; 
    •  

    沒有留言: