一、数据集添加序号
方法1:data步生成
方法2:proc sql生成
/** 方法1 **/
data class1;
set sashelp.class;
seq = _N_; /** 添加序号 **/
run;
/** 方法2 **/
proc sql;
create table class2 as
select monotonic() as id, * /** monotonic()添加id **/
from sashelp.class ;
quit;
二、同一ID多条观测加序号
第一步:排序 proc sort
第二步:first语句、累加
data t1;
input id name$ sex$ age subject$ score;
cards;
1001 Tom F 25 Chinese 80
1001 Tom F 25 Math 72
1001 Tom F 25 English 90
1003 Lisa M 20 Chinese 65
1003 Lisa M 20 Math 93
1005 Anna M 26 Chinese 93
1005 Anna M 26 Math 70
1002 Lyli M 21 Chinese 85
;
run;
proc print data=t1;run;
proc sort data=t1; by id subject; run;
data t2;
set t1;
by id subject ; /* 与排序的变量一致 */
retain SEQ;
if first.id then SEQ = 1 ;
else SEQ = SEQ +1 ;
run;
proc print data=t2;run;