SAS抓住以前的观察结果进行比较

时间:2014-10-02 14:50:58

标签: sas proc-sql

我有一个类似于:

的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中是否可以返回先前的观察结果,这可能是proc sql的问题,但是我需要新的data set而不是表格。

1 个答案:

答案 0 :(得分:4)

如果名称只出现一次,即你不能有账单,账单,账单,乔,乔,账单,账单,那么请使用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;