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;

沒有留言: