看板 Statistics 關於我們 聯絡資訊
※ 引述《methylin ()》之銘言: : [軟體程式類別]: : sas 9.4 : [程式問題]: : 資料處理 : [軟體熟悉度]: : 新手 (不太熟悉 SQL 語法) : [問題敘述]: : 原始資料格式如以下示意圖 : ID Month : 1 1 : 1 2 : 1 3 : 1 6 : 1 9 : 1 10 : 2 2 : 2 3 : 2 4 : ..... : 我想要把資料整理成以下的格式: : ID start_month end_month : 1 1 3 : 1 6 6 : 1 9 10 : 2 2 4 : .... : 若同一個人的月份資料是連續的,歸為同一個episode,若有中斷則歸到新的episode : 這似乎是一個很簡單的問題,但真的讓我很頭大 : 還請版上的先進幫忙解答,非常感謝! data a; input ID Month; datalines; 1 1 1 2 1 3 1 6 1 9 1 10 2 2 2 3 2 4 ; run; proc sort data=a;by id month; run; data a; set a;by id; x=dif(month); retain order 0; if x^=1 then order=order+1; if first.id then order=1; run; proc means data=a noprint; var month; by id order; output out=b(drop=_type_ _freq_ order) min=start_month max=end_month; run; proc print data=b; run; -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.48.83.83 ※ 文章網址: https://www.ptt.cc/bbs/Statistics/M.1488797695.A.DA4.html
methylin: 感謝,來試試看 03/06 20:17
anniecs: 推推 邏輯很好 寫得很棒 03/07 20:04