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

SAS抓取以前的观察结果进行比较,sas,proc-sql,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数据集类似于:

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的问题,但我需要一个新的数据集而不是表。

如果名称只出现一次,即不能有一条条纹,即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;

如果名称只出现一次,即您不能有一个连胜,即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;

@Kavan供您参考,如果这是一个家庭作业/课堂作业问题,您可能应该在问题中注意一些相关信息。SAS有一些惊人的元素,这意味着我们可以给你一些相当复杂的答案,因此让我们知道上下文有助于我们给出有用的答案,也许会提醒回答者给出更完整的答案我们应该在答案中始终包含这些信息,但有时我们会忘记。数组在这里是非常错误的方式。在大多数其他语言中,这是有意义的,但由于SAS逐行处理数据,数组通常用于处理单行数据,而不是一列数据。@Kavan仅供参考,如果这是一个家庭作业/课堂作业问题,您可能应该在问题中记下一些相关信息。SAS有一些惊人的元素,这意味着我们可以给你一些相当复杂的答案,因此让我们知道上下文有助于我们给出有用的答案,也许会提醒回答者给出更完整的答案我们应该在答案中始终包含这些信息,但有时我们会忘记。数组在这里是非常错误的方式。在大多数其他语言中,这是有意义的,但由于SAS逐行处理数据,因此通常使用数组处理单行数据,而不是一列数据。