如果名称只会出现一次,即你不能有一个连胜,那就是 bill, bill, joe, joe, bill, bill 然后使用 proc freq:
proc freq data=have;
table name/out=want;
run;
否则,使用带有计数器的 notsorted 选项。
data want;
set have;
*BY tells sas my data has groups according to the "Name" variable ,
with the notsorted meaning that it isn't an alphabetical or numerical order
basically, whenever the Name changes its a new group;
by name notsorted;
*Keep the value across the different rows rather than reset;
retain counter;
*if this is the first of a group then set the counter to 1;
if first.name then counter=1;
*If this isn't the first name in the group then increment the counter,
retain means its kept the value from previous row;
else counter+1;
*If this is the last of the "Name" group then output the observation;
if last.name then output;
run;