SAS 计数观察

SAS counting observations

我正在查看的数据集看起来像

GVKEY 日期年份

1 001000 1971

2 001000 1972

3 001000 1973

4 001000 1974

5 001000 1975

6 001000 1976

7 001001 1971

.

.

.

88 001010 1971

89 001010 1972

90 001010 1973

.

.

.

105 001010 1988

106 001010 1989

107 001011 1973

.

.

.

所以我只想保留从 1971 年到 1989 年的 DateYear 中没有间隙的 GVKEY。我想我必须删除重复项,但是我将如何计算每个 GVKEY 的观察值并删除不存在的 GVKEY有 19 个观察结果? 这在 SAS 中可行吗? 谢谢。

Proc SQL 中,您可以计算一组中的不同值以识别具有 19 个不同年份的键。

示例:

proc sql;
  create table want as
  select distinct key, year
  from have
  where year between 1971 and 1989
  group by key
  having count(distinct year) = 19
  order by key, year
  ;

在数据步骤中执行此操作的直接方法是双 DoW 循环。循环遍历数据两次;对于任何给定的 ID,首先它会遍历该 ID 的所有记录,检查它是否符合标准(在这种情况下,删除,尽管你可以做相反的事情),然后第二次循环并应用适当的逻辑输出或删除。

data want;
  do _n_ = 1 by 1 until (last.id);
    set have;
    by id;
    if [logic] then delete_flag=1;
  end;
  do _n_ = 1 by 1 until (last.id);
    set have;
    by id;
    if delete_flag ne 1 then output;
  end;
run;

您只需要确定设置该删除标志的逻辑是什么。在你的情况下,它可能是使用 if first.id and year ne 1971if last.id and year ne 1989if dif(year) gt 1 的某种组合,但你的问题不够清楚,无法明确说明你在这里做什么 - 但是希望你能解决这个问题,或者更新更多细节。