作者bugle (低點)
站內Statistics
標題Re: [程式] sas 新變數問題
時間Fri Dec 25 07:32:17 2009
※ 引述《lsshno1 (朝右邊鋒邁進)》之銘言:
: 大家好!
: 我在sas遇到一個小問題
: 假設我的資料如下
: ID R
: 1 2
: 1 3
: 1 1
: 2 3
: 2 10
: 3 4
: 3 11
: 3 5
: 3 7
: 我希望我的ID後面都有一個變數叫做Weight ,
: 他的定義是 ID = i 的個數
: 例如 ID =1 時 , 所有ID=1的變數Weight都=3
: 所以我的資料變成這樣
: ID R weight
: 1 2 3
: 1 3 3
: 1 1 3
: 2 3 2
: 2 10 2
: 3 4 4
: 3 11 4
: 3 5 4
: 3 7 4
: 雖然很簡單就是快要想出來可以又想不到!
: 感謝大家!!
提供data step的作法
proc sort data=XXX; by ID; run;
data XXX1;
set XXX;
by ID;
retain weight 0;
if first.id then weight = 0;
weight + 1;
if last.ID;
run;
data XXX;
merge XXX XXX1(keep=ID weight);
by ID;
run;
--
try it
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.127.203
推 wlsherica:推 12/25 14:30
推 kenshin528:也可以用proc freq做 12/25 15:41
推 lsshno1:tks!我自己後來用proc freq做出來了 12/26 00:09