Re: [問題]SAS類別變項的累加次數

看板Statistics作者 (咖啡王子)時間16年前 (2010/03/13 01:32), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《wlsherica (Mon_tresor)》之銘言: : ※ 引述《anyoilk (黑鼻頭)》之銘言: : : 現在我想把資料中的OWN變項(類別)內容筆數相加,變為下面: : : ID own NO : : 甲 1 1 : : 甲 1 2 : : 甲 2 1 : : 乙 1 1 : : 乙 2 1 : : 丙 1 1 : : 丙 1 2 : : 丙 2 1 : : 丙 2 2 : : 而我是試過下面的語法 : : data B; : : set A; : : by id; : : if first.id then NO=0; : : NO+own; : : run; : : 但跑出來,卻變成 : : ID own NO : : 甲 1 1 : : 甲 1 2 : : 甲 2 4 : : 乙 1 1 : : 乙 2 2 : : 丙 1 1 : : 丙 1 2 : : 丙 2 4 : : 丙 2 6 : : 我想破頭了,但還是卡住,請各位救救我,謝謝。 : 因為您提供的程式會一直累加, 試試看下面的語法: : proc sort data=a; /*原始檔名稱a*/ : by ID own; : data b; : set a; : by ID own; : if first.own=1 then no=0; : no+1; : output; : if last.own; : run; : proc print;run; 假設資料順序一開始就是對的 則在讀進資料以後 以下的語法就可以滿足你想要的 data a; set a; retain no 0; no=no+1; if id^=lag(id) then no=1; if own^=lag(own) then no=1; run; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.44.204.168

03/13 13:58, , 1F
感謝t大,這個方法也成功了
03/13 13:58, 1F
文章代碼(AID): #1BcdgfE7 (Statistics)
文章代碼(AID): #1BcdgfE7 (Statistics)