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;

2014年4月27日 星期日

建構變項—SAS Funtion

function-name(argument-1<,argument-n>);

  • mean(x1,x2,x3)
  • mean(of x1-x3)
    • mean(x1-x3)變成 x1-x3 的平均
  • mean(of narray{*})

 

文字與數值之間轉換

  • 多數的function使用時,系統可自行轉換,建立一個暫時變項,但遇到 $ , 等情況會失效
  • Input 可 Character-to-Numeric Conversion
    • INPUT(source, informat)
      New=Input ( ID ,  4. )
  • Put 可 Numeric-to-Character Conversion
    • PUT (source,format)
      • Fee_lv2=put( fee, 3.);
      • Fee_lv2=put( fee, fee.);
      • Fee_lv=input(  put( fee, fee.),  $10. );

========================
與 Date Value 有關的 function
========================

  • now1=mdy(4,27,2014); format now1 date9.;
    now2='27Apr2014'd;    format now2 date9.;
  • now3=today(); format now3 mmddyy10.;
    now4=date(); format now4 mmddyy8.;
  • nowtime=time(); format nowtime TIME8.;
    nowtime2='27Apr2014:15:35:00'dt; 
    format nowtime2 datetime.;

now1=
mdy(4,27,2014);

不正確的用法
mdy(Apr,27,2014);
mdy(‘Apr’,27,2014);

format now1 date9.;

27APR2014

now2=
'27Apr2014'd;

format now2 date7.;

27APR14

now3=today();




now4=date();

format now3 mmddyy10.;

format now4 mmddyy8.;

04/27/2014

04/27/14

nowtime=time();

format nowtime TIME8.;

15:35:17

nowtime2=
'27Apr2014:15:35:00'dt;

format nowtime2 datetime.;

27APR14:15:35:00

 

  • day=day(now1); /*27APR2014*/
    27
    month=month(now1);
    4
    year=year(now1);
    2014
  • qtr=qtr(now1);
    2
  • weekday=weekday(now1);
    1 /*日一二三四五六*/

 

  • 時間間隔
    INTCK('interval',from,to) 
    INTCK ( 'day' , '31dec2013'd , '01jan2014'd );
    常用於算周年
    • Day  經過幾個日數----------  1日
    • Week 經過幾個Sunday ----- 0個 Sunday
      • from為周日,to為周一,則0個Sunday
      • from為周六,to為周日,則1個Sunday
    • Month 經過幾個某月1日------1個某月1日
    • Year 經過幾個1月1日---------1個1月1日
      • Years = intck ( ‘year’, First_meet, Today() );

 

    • 時間推移
      INTNX('interval',startfrom,
                             increment<,'alignment'>)
      'alignment' 可為 b m e s,用於 month較易懂,其他interval 的不是很懂
      • Month
        • MonthX=intnx ('month','27apr2014'd, 5 , 'alignment' );
          推 5個月,b m e s的結果依序為
          01SEP2014月初
          15SEP2014月中
          30SEP2014月尾
          27SEP2014同日

 

日期之差

  • DATDIF(start_date,end_date,basis)
    • basis 有
      '30/360'   'ACT/ACT' 
  • YRDIF(start_date,end_date,basis)
    • basis 有
      '30/360'   'ACT/ACT'   'ACT/360'   'ACT/365'

 

=========================
與 文字 有關的 function
========================

傳出變項裡的字串,或者取代字串

  • SCAN(argument,n<,delimiters>)
    • 依 delimiter 區分,來選字第n個字,leading delimiters have no effect,預設的delimiter 有
      blank . < ( + | & ! $ * ) ; ^ - / , %
    • SCAN產生的新變項預設為 $200. 
      最好先設定 LENGTH
    • 新變項=scan (Name , 2,  ' -'  );
      delimiter 是 空白 與 -
    • 新變項=scan (Name , 2,  '-'  );
      delimiter 只有 -  
  • SUBSTR (argument, position <,n>)
    n表示連著取n個字元,不指定 n 就是position之後全要了。數值變項,請先Put成文字變項。
    • 依 position 來選字
      • First=Substr ( name , 1, 5) ;
    • 依 position 來取代文字,類似 TRANWRD
      • Substr ( name , 1, 5  ) ='First';
      • 有意義一點的作法是
        IF Substr ( name , 1, 1  ) = 'M'
             then Substr ( name , 1, 2  ) ='W%';
        注意新增的文字的 n 要對,本例為2個字元。
  • TRANWRD(source,target,replacement)
    搜查特定文字,並取代該文字。記得設定 length
    • Nname=TRANWRD ( name , 'M' ,  'W%' );

 

刪除多餘空白

  • TRIM(argument)
    刪除trailing blanks,用於組合文字
    新變項的length有可能多於實際文字量,因此還是會有trailing blanks
    • new=trim(Lastname) || ', ' || trim(location) || ', ' || trim(phone) ;
  • CATX(separator,string-1 <,...string-n>)
    • 用於連結字串,移除 leading and trailing blanks, 插入分隔符號( separator)。
      相當於TRIM and LEFT 的組合
    • new2=catx ( ', ' , Lastname, location, phone);

 

找字串的Position

  • INDEX(source,excerpt)
    搜尋特定字串(case sensitive),並回傳找到第一個的位置,若找無就是0
    • n1=INDEX (Lastname,'LA') ;
  • FIND(string,substring<,modifiers><,startpos> )
    用途與INDEX相近
    • n2=Find (Lastname,'LA') ;

 

大小寫文字的轉換

    • UPCASE(argument) 全大寫
    • LOWCASE(argument) 全小寫
    • PROPCASE(argument<,delimiter(s)>) 依分隔點區分,首字元大寫

 

========================
與 數字 有關的 function
========================

INT(argument) 取整數部分

ROUND(argument,round-off-unit) 類似四捨五入

  • d1 = round(1234.56789,100)     - 1200;
    d2 = round(1234.56789,10)      - 1230;
    d3 = round(1234.56789,1)       - 1235;
    d4 = round(1234.56789,.1)      - 1234.6;
    d5 = round(1234.56789,.01)     - 1234.57;
    d6 = round(1234.56789,.001)    - 1234.568; 
    d7 = round(1234.56789,.0001)   - 1234.5679;
    d8 = round(1234.56789,.00001)  - 1234.56789;

2014年4月26日 星期六

結合SAS Data Set

結合SAS Data Set
  • One-to-one merging (應該改很少用 @@)
    • 新資料檔有所有的變項,兩資料檔的數據依序進入 PDV,因此相同變項時,第1資料檔會被覆寫,保留第 2資料檔的數據。
    • Observation 只留「最少」資料檔的筆數。PDV 會進第1資料檔的第3筆,但是讀第2資料檔時會遇到 end-of-file,PDV就停止進資料,也不會輸出到新檔案中。
    • 用 Set
    • Data ab ;
      set a ;
      set b ;

      run;
      • Data-set-A 
        Num    X
        1          10
        2          15
        3          20
      • Data-set-B 
        Num       Y
        11           6
        13          12
      • Combined Data-set
        Num    X        Y
        11       10        6
        13       15       12
    • Data ab ; Merge a b; 則是會保留「最多」的Observation。
  • Concatenating 垂直連接,成為新資料檔
    • 新資料檔有所有的變項,也有所有的 Observation
    • 同變項,要有相同的 attribute,若是 type 不同( NUM 與 Char) 會有 Error;若是同屬性,但length, Label, format, informat 不同,會以先讀入的為主。
    • 用 SET 
    • Data ab ;
      set  a  b;
      run;
      • Data-set-A 
        Num    X
        1          10
        2          15
        3          20
      • Data-set-B 
        Num    Y
        11           6
        13          12
      • Combined Data-set
        Num    X        Y
        1          10        .
        2          15        .
        3          20        .
        11            .         6
        13            .        12
  • Appending 附加,以原來 BASE為主,加在尾部
    • PROC APPEND                            
                 BASE=Base-data-set          DATA=Second-data-set  FORCE;
      RUN;
    • 將 Second-data-set 的數據是加在 Master( Base) data-set 的尾部,沒有產生新 data-set,也不去讀取  Master( Base) data-set。
    • 一次 Procedure 只能處理 2 個資料檔,Base 與 Second。
    • 若 Second-data-set  的變項 有下列情況,要用 FORCE option;
      • 有新的變項,不在 Base-data-set 裡
        • Base沒有的變項會被 drop。
      • 同名變項 的 type 不同, length 不同。
        • 維持 Base 的type, Second-data-set的數據變missing;
          維持 Base 的Length,Second-data-set的數據被刪截;
  • Interleaving 插入數據 ( Concatenating 加入 BY )
    • 所有資料必須先 Sort in Ascending order.
    • Set, By
      Data ab ; set a b ; by num; run;
      • Data-set-A
        Num     X
        1          10
        2          15
        3          20
      • Data-set-B
        Num     X   Y
        1           6    6
        3          12   12
      • Combined Data-set
        Num     X    Y
        1          10     .
        1            6     6
        2          15     .
        3          20     .
        3          12    12
  • Match-merging
    • MERGE, BY
      • 最好都先排序成 Ascending order,By值是missing排在頂,Execution Phase: PDV會依照BY值序順進行比對。
      • 變項屬性以第 1個資料檔為主,但變項值則會被後到的資料 overwritten。
      • 變項改名字
        • Data-set ( RENAME= ( old-variable-name=new-variable-name ) ) 
    • Excluding Unmatched Observations
      • (IN= data set option) 與 If statement,產生 temp_a, temp_b 兩個 temporary variable,再利用 If statement 進行篩選所要輸出的資料檔
        • 全部資料都留
          Data Mab ;
          merge  a ( in= temp_a)   b ( in=temp_b  rename=(x=new_x) );
          by Num;
          If     temp_a or temp_b  ;    *或者不寫 If statement;
          run;
        • 只留完全 match ,AB兩資料檔都有的,把 if 段改如下
          If     temp_a and  temp_b  ;
          或者
          If     temp_a =1 and  temp_b  =1 ;
        • 保留只在 A  或是 只在 B的
          If     temp_a ;
          If     temp_b  ;

2014年4月25日 星期五

SAS 資料處理技巧1

變項Keep 與 Drop
  • data work (drop=age group);
  • set clinic    (keep=age group BMI );
    • 讀檔時 (SET) 只進 age group BMI ,新資檔(DATA) 刪除 age group,最後輸出只有BMI


FIRST.變項 與 LAST.變項
  • 一定要先Proc Sort
  • Proc Sort 之後,遇到 Data ; by X Y;,在program data vector裡會自動建立FIRST.變項 與 LAST.變項,First.X Last.X First.Y Last.Y,可參考下表
Department FIRST.Department LAST.Department
A 1 0
A 0 0
A 0 1
B 1 0
B 0 1
C 1 1

範例如下:
data salaries2 ; set salaries ;
by Department;
/*第一筆設定為0*/ 
if first.Department then Payroll=0;
/*累加*/
      payroll+yearly;
/*是最後一筆再輸出*/
if last.Department;
run;

Using Direct Access:指定讀特定一筆數據,並輸出。(Point-Output-Stop)
  • Point 與 OUTPUT 聯用。通常用於取得Random sample。
    • data test;
          obsnum=3; /*自定變項與值*/
          set salaries  point=obsnum;  /* 被 point 變項就消失 */
          N=1;
          OUTPUT;  /*強制輸出到實體資料檔*/
          Stop;     /*找到某筆數據後,停止 iteration,不
                          必等到 Mark of the end */
      K=1;   /*因為在OUTPUT之後,數值不會被輸出*/
      run;

資料列末端的技巧 ( End=var )
  • 用途:輸出資料檔的最未一筆總計。(似乎用Last.即可)
  • End=var,建立暫時變項 var ,若 SET statemnet 讀到了 end of file,var就為1
  • END= 不與 POINT= 一起用
    • set sasuser.stress2(keep=timemin timesec) end=last;
    • .........
    • if last; /* 當 last = 1 才輸出 */

SAS DATA SET 的處理流程,與讀 Raw data file 差不多,主要差異如下:
  • Execution Phase:第一筆資料輸出到新資料檔後,會在 Program Data Vector裡 Retain 保留 SET statement 已讀入的 (如:第一筆資料) 與 sum statement 所產生的變項,一直到再執行 SET statement,讀入第二筆數據
    • 但讀 RAW DATA FILE 則是重設為 MISSING,除非是RETAIN, SUM STATEMENT變項, 暫時的ARRARY,FILE或INFILE的OPTION建立的變項,自動變項

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);

2014年4月23日 星期三

準備Base programming 筆記_HTML Output

ODS 有很多 Destinations,其中常用的是 LISTING, HTML, OUTPUT, PDF 等。

輸出 ODS HTML
  1. ODS HTML BODY="file-specification"   STYLE=style-name;               
                    /* BODY 可用 FILE*/
  2. ODS HTML BODY=fileref;  * 特定檔案才可;
  3. ODS HTML PATH=mine.mycat BODY=name.HTML;
    • procedure 1 ~ procedure N
  • ODS HTML CLOSE;


多張表,製作 Table of Contents
  • 以 frame.html 為主,裡頭紀錄 content 與 body的路徑,因此有相對、絕對路徑兩種設定方式,讀檔時按 frame.html即可。
  • 相對路徑的寫法 1  (檔案一起搬到別的資料匣也能正確開啟)
    • ods html   
    • body="z:\DATA\data.html"       (url='data.html') 
    • contents="z:\DATA\toc.html"   (url='toc.html')
    • frame="z:\DATA\frame.html";
  • 相對路徑的寫法 2
    • ods html   path="z:\DATA\"  (url=none)
    • body="data.html"    
    • contents="toc.html" 
    • frame="frame.html";
  • 絕對路徑的寫法
    •  (URL='某個真的 url') 
    • 需再將三個檔案部署到正確的網址

2014年4月22日 星期二

準備Base programming 筆記_簡單的分析統計

PROC MEANS <DATA=SAS-data-set>
<statistic-keyword(s)> <option(s)>;
  • 預設會出報表,但可用NOPRINT 不出報表。
  • 報表的小數位
    • proc means data=sasuser.diabetes min max maxdec=1;
      run;
  • Group Processing,Class 與 BY
    • Class 不必先 sort,報表集合為一張
    • By 必先sort,報表分成N張,但總分層較多時,速度較快
  • OUTPUT OUT=SAS-data-set STATISTIC=new_variable(s);
    • var arterial heart cardiac urinary;
      output out=test mean=Ave_1 Ave_2 ;
      • Ave_1 Ave_2 是依VAR的順序,先到先贏,即arterial heart ,而 cardiac urinary 就沒有輸出。
      • STATISTIC=new_variable(s) 可指定不只一組,不指定就是全部變項的全部統計值都輸出。

PROC SUMMARY
  • 大致用法同上,但預設無報表,要用 PRINT (option)才會出表

PROC FREQ
  • / nofreq nopercent norow nocol missing
  • tables sex*weight*height;
    N way table 會先以 sex進行分層,輸出weight*height的表(frequency, percent, row pct, column pct)
  • tables sex*weight*height / list;
    只輸出 frequency,與其相關的percent, cumulative frequency, cumulative percent
  • tables sex*weight*height / crosslist;
    以List的方式,輸出與N way table相同的表,支援用 Proc template進行修改。

2014年4月21日 星期一

數字轉文字並保留小數位數

/*數字轉文字並保留小數位數 Converting a Numeric Variable to a Character Variable*/
data tt;
b=131.19;
b1=compress(b);
b2=put(b,6.2);
a=130.00;
a1=compress(a);
a2=put(a,6.2);
run;
image
之前都用 compress的方式,但最大問題是剛好整數(130.00)會變成130!有想過以 length 取得長度,但數值的長度與是否為整數無關,一開始指定best12. ,該欄下的所有 length 就都是12。
因此改用 put 最方便,PUT always return a character
其中參數6.2是指先保留6個位元,其中含兩位小數,輸出後的文字總長即為6位元。

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; 
    •