Re: [程式] SAS 資料整理問題請益

看板Statistics作者 (咖啡王子)時間7年前 (2017/03/06 18:54), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《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

03/06 20:17, , 1F
感謝,來試試看
03/06 20:17, 1F

03/07 20:04, , 2F
推推 邏輯很好 寫得很棒
03/07 20:04, 2F
文章代碼(AID): #1OlJ__sa (Statistics)
文章代碼(AID): #1OlJ__sa (Statistics)