1

我有一个类似于以下的 SAS 数据集:

Joe
Joe
John
Bill
Bill
Bill
Bill
Joanne
Joanne
Fred
Fred
Fred
Ted

我想要做的是将它变成一个新的数据集,每个条目只有一次,并添加一个变量来存储该变量的“条纹”。例如,集合看起来像:

Joe     2
Bill    4
Joanne  2
Fred    3
Ted     1

到目前为止,我已经创建了第一个数据集,但我被困在如何做第二个数据集上。我的想法是在数据步骤中创建一个变量,该变量保存最后一次观察的值以进行比较,但我不知道如何做到这一点,或者如何将其转换为第二个数据集。

我不确定在 SAS 中是否可以返回之前的观察,也许这对于 a 来说是个问题proc sql,但是我需要一个新data set的而不是表。

4

1 回答 1

4

如果名称只会出现一次,即你不能有一个连胜,那就是 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;
于 2014-10-02T14:59:35.957 回答