看板 Statistics 關於我們 聯絡資訊
很謝謝west1996的回覆~ 還有另外一個低調的板友也有寄給我~ 寫法都有一些差異 我一開始想到做法是用PROC SQL中的subquery做 雖然已經有人寫出來了 不過懷著運動家的精神還是把它寫完 之前我卡在 如果前後同時有一樣接近的點 該取哪一個的問題 因為subquery只能回傳一個值 後來我參考了west1996的做法 多產生一個變項 (真是太感謝了) 每次多看別人怎麼寫 自己也都會有進步!!! 我重新舉了一個比較複雜的例子(包括前後距離相同) 分別抓第6個月和第12個月之前後一個月內最接近值 以下 跟大家分享: DATA TEST1; Input N PID Time AFP ALT; cards; 1 1 0.1 100 10 2 1 2 48 20 3 1 5.8 45 30 4 1 6.2 11.0 . 5 1 10 8.9 50 6 1 11.3 4.7 60 7 1 12.1 4.5 . 8 1 13 4.22 71 9 1 15 3.45 72 10 1 18.2 2.13 73 11 1 31 2.12 74 12 2 0 30 24 13 2 3.5 20 25 14 2 5.9 10 26 15 2 6.3 4 27 16 2 11.8 5 20 17 2 12.9 6 30 18 3 0 5.5 40 19 3 1 2.5 50 20 3 2 4.2 60 21 3 12.1 . 20 22 3 12.5 3.5 30 RUN; PROC sort data=test1 (keep= PID) out=test3 nodupkey; By PID; RUN; %MACRO ALLTIME(VAR,T); DATA test2; SET test1; where &VAR.~=.; RUN; Proc sql; create table want1 as select test3.PID , (select &VAR. from test2 where PID=test3.PID having test2.Time<=&T. and ABS(test2.Time-&T.) = MIN(ABS(test2.Time-&T.))) as &VAR.&T.M_less ,(select time from test2 where PID=test3.PID having test2.Time<=&T. and ABS(test2.Time-&T.) = MIN(ABS(test2.Time-&T.))) as &VAR.&T.M_time_less , (select &VAR. from test2 where PID=test3.PID having test2.Time>&T. and ABS(test2.Time-&T.) = MIN(ABS(test2.Time-&T.))) as &VAR.&T.M_over ,(select time from test2 where PID=test3.PID having test2.Time>&T. and ABS(test2.Time-&T.) = MIN(ABS(test2.Time-&T.))) as &VAR.&T.M_time_over from test3 ; quit; DATA want1; SET want1; IF &VAR.&T.M_less=. and &VAR.&T.M_over=. then Do; &VAR._&T.M=.; &VAR._&T.M_time=.; END; ELSE IF &VAR.&T.M_less~=. and &VAR.&T.M_over=. then Do; &VAR._&T.M=&VAR.&T.M_less;&VAR._&T.M_time=&VAR.&T.M_time_less; END; ELSE IF &VAR.&T.M_less=. and &VAR.&T.M_over~=. then DO; &VAR._&T.M=&VAR.&T.M_over;&VAR._&T.M_time=&VAR.&T.M_time_over; END; ELSE IF &VAR.&T.M_less~=. and &VAR.&T.M_over~=. then DO; &VAR._&T.M=(&VAR.&T.M_less+&VAR.&T.M_over)/2; &VAR._&T.M_time=9999; &VAR._&T.M_time2=CATS(&VAR.&T.M_time_less,"&",&VAR.&T.M_time_over); END; IF &VAR._&T.M_time=9999.0 then &VAR.&T.M =CATS(&VAR._&T.M,"(", &VAR._&T.M_time2 ,")"); ELSE IF &VAR._&T.M_time <&T.-1 or &VAR._&T.M_time >&T+1 then &VAR.&T.M= . ; ELSE &VAR.&T.M =CATS(&VAR._&T.M,"(", &VAR._&T.M_time ,")"); keep PID &VAR.&T.M; RUN; DATA test3; Merge test3 want1; By PID; RUN; %MEND ALLTIME; %ALLTIME(AFP, 6) %ALLTIME(AFP, 12) %ALLTIME(ALT, 6) %ALLTIME(ALT, 12) PROC PRINT data=test1;RUN; PROC PRINT data=test3;RUN; ※ 引述《west1996 ()》之銘言: : 注意:沒有data可以測,憑想像空打,程式碼可能有錯,請自行debug一下 : 這支macro可以一次轉一個變數一個時間點,餵給他三個東西 : 1. 資料集名稱 : 2. 變數名稱 : 3. 月數(要給一個數字) : macro會產生該名稱該變數的結果在work裡,程式碼最後一行的範例是把yourdata這個 : 資料集的GOT變數的3個月資訊轉出來存成work.out_GOT_3M : P.S.要一次轉多個變數多個時間點需要另外加工包裝macro,比較麻煩,既然你的欄位 : 不多,時間點也不多,data也不大,所以乾脆多跑幾次macro再自行merge : 資料就好,速度上不會差很多 : 程式碼如下: : %macro aggregatedata(dsn=, var=, month=); : data work.out_&var._&month.M; : set &dsn.; : by id; : retain temp_value temp_month over_value over_month over_flag; : length &var._&month.M $ 12; : if first.id=1 then do; temp_value=.; temp_month=.; over_value=.; over_month=.; over_flag=0; end; : if &month.-1<= month <= &month.+1 and &var. ^= . and temp_month ^= &month. and over_flag ^= 1 then do; : if month <= &month. then do; : temp_value=&var.; : temp_month=month; : end; : else if month> &month. then do; : over_value=&var.; : over_month=month; : over_flag=1; : end; : end; : if last.id=1 then do; : if temp_value = . and over_value= . then &var._&month.M='.'; : else if temp_value=. and over_value ^=. then &var._&month.M=cats(over_value,'(',over_month,')'); : else if temp_month = &month. then &var._&month.M=cats(temp_value,'(',temp_month,')'); : else if &month.-temp_month <= over_month-&month. then &var._&month.M=cats(temp_value,'(',temp_month,')'); : else if &month.-temp_month > over_month-&month. then &var._&month.M=cats(over_value,'(',over_month,')'); : end; : output; : end; : keep id &var._&month.M; : run; : %mend; : %aggregatedata(dsn=yourdata, var=GOT, month=3) : ※ 引述《yonny (悠逆)》之銘言: : : [軟體程式類別]: SAS (R也可以 但是我跟R比較不熟) : : [程式問題]: 資料處理 : : [軟體熟悉度]:熟悉 : : [問題敘述]: : : 我的資料大概長像這樣 : : No是每次檢驗的流水號 ID相同是同一個人 : : GOT和GPT是在不同時間點做的檢驗數值 : : 但不是每個時間點 都有兩個檢查都做 : : 而且不同人做檢查的時間點也都不相同 : : No ID Month GOT GPT : : 1 1 0.2 41 34 : : 2 1 2.8 42 . : : 3 1 3.5 43 36 : : 4 1 3.7 44 37 : : 5 1 4.9 45 38 : : 6 1 5.5 51 39 : : 7 1 5.7 52 40 : : 8 1 6.0 53 . : : 9 1 6.2 54 . : : 10 2 0.9 20 21 : : 11 2 4.0 22 . : : 12 2 4.1 24 25 : : 13 2 5.7 . 36 : : 14 2 6.0 30 . : : 以下是我期望的資料型態 : : ID GOT_0M GOT_3M GOT_6M GPT_0M GPT_3M GPT_6M : : 1 41(0.2) 42(2.8) 53(6.0) 34(0.2) 36(3.5) 40(5.7) : : 2 20(0.9) 22(4.0) 30(6.0) 21(0.9) . 36(5.7) : : 我想要把資料轉置成橫的,以GOT為例, : : 我要創造三個變項 GOT_0M GOT_3M GOT_6M (分別為0,3,6個月的GOT) : : 這三個時間點若無完全符合的值, 則抓正負一個月內的值來代替 : : 若正負一個月內都無值, 則missing : : 值後面括號(是用哪個時間點的值) : : --- : : 以上大概是我的資料型態 : : 實際的資料 有好幾百人的資料 : : 每個人大概都有30~100個檢驗的時間點 : : 檢驗數值(如GOT,GPT之類的) 大概有10幾個 : : 而我們要取的時間點實際上也不只有三個(0,3,6,12,18,24,36,48M) : : 感覺有點複雜(囧) : : 我目前大概查一下 可能用PROC SQL先寫出來 : : 再看改成MACRO : : (我今天掙扎了一下午 只寫了一點點簡單的 完全離目標很遠XD) : : 想請問看板上的高手有無建議要用什麼statement寫比較適合? : : 我再研究一下 : : 或是若有高手可以幫忙寫出來 : : 小小3000P表達感謝! : : 非常謝謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.224.48.248 ※ 文章網址: https://www.ptt.cc/bbs/Statistics/M.1459003870.A.437.html
yonny: PS. 如果前後值相同 我寫成求平均~ 03/26 22:54