過去曾經介紹過以SASIFN(), LAG()等函數將資料向下垂直移動,但SAS可否有LAG()函數的相反函數,也就是將資料垂直向上移動的函數,答案應該是沒有的,但可透過PROC EXPAND程序執行資料的垂直向上以及向下的移動,甚至是移動數個列數,該語法屬於Time series(時間序列)的應用。

【程式一】建立範例資料,資料中包含ID(身份證號)sex(性別)Birthday(生日)date (就醫日期)

 

【程式一】

data aa;
        input id $ sex $ birthday yymmdd10. +1 date yymmdd10.;
        format birthday date yymmdd10.;
cards;
A01 F 1958-01-04 2001-02-03
A01 F 1958-01-04 2004-05-06
A02 M 1964-07-08 2004-12-11
A02 M 1964-07-08 2005-01-03
A02 M 1964-07-08 2006-11-13 
;
proc print;
run;

 

【程式二】

以下利用proc expand 進行資料的向上與向下移動,移動後的資料檔名為work.bb,移動的依據為IDbirthday。再利用convert指定將資料作轉換,convert的指令為CONVERT variable = newname … </options>;  等號左邊放要被轉換的變項,等號右邊為轉換後的變項透過transformout=(lag)可以將資料往下移動,transformout=(lead)將資料往上移動。若在laglead後面加上數字,則是移動的列數。也可以利用id指令取代by而作為資料移動的依據,但是id指令後面只能放數值型資料,包括日期格式的資料。

 

/*-移動日期--*/
proc expand data=aa out=bb method = none; 
        by id birthday;
        convert date = date_lag1 / transformout=(lag); 
        convert date = date_lead1 / transformout=(lead); 
        convert date = date_lag2 / transformout=(lag 2); 
        convert date = date_lead2 / transformout=(lead 2); 
proc print;
run;

 

【結果】

proc_expand  

 Reference

http://goo.gl/q0mM1R

arrow
arrow
    全站熱搜

    estat 發表在 痞客邦 留言(0) 人氣()