SAS 按 ID 输出每个最大值
SAS Output Each Max Value By ID
我觉得我把它弄得比应该的更复杂了。我在下面有一个示例数据集,其中包含一个 ID 列和一个计数器列。计数器列重置,我想创建一个数据集,其中仅包含计数器列在再次重置之前为最大值的行。我的数据集还有数千个 ID,我需要为此执行此操作。
data test;
infile datalines delimiter=",";
informat ID .
TCOUNT 10.;
input ID $ TCOUNT $ ;
datalines;
123,1
123,2
123,3
123,4
123,1
123,2
123,3
123,1
123,2
;
run;
我想要的新 table 输出看起来像...
ID TCOUNT
123 4
123 3
123 2
可能 easiest/clearest 首先为每个非递减 TCOUNT 个观察块分配一个标签。
data groups;
set test;
by id ;
if first.id then group=0;
if first.id or tcount<lag(tcount) then group+1;
run;
然后找到每个组中的最后一个观察值是一件简单的事情。
data want;
set groups;
by id group;
if last.group;
run;
我觉得我把它弄得比应该的更复杂了。我在下面有一个示例数据集,其中包含一个 ID 列和一个计数器列。计数器列重置,我想创建一个数据集,其中仅包含计数器列在再次重置之前为最大值的行。我的数据集还有数千个 ID,我需要为此执行此操作。
data test;
infile datalines delimiter=",";
informat ID .
TCOUNT 10.;
input ID $ TCOUNT $ ;
datalines;
123,1
123,2
123,3
123,4
123,1
123,2
123,3
123,1
123,2
;
run;
我想要的新 table 输出看起来像...
ID TCOUNT
123 4
123 3
123 2
可能 easiest/clearest 首先为每个非递减 TCOUNT 个观察块分配一个标签。
data groups;
set test;
by id ;
if first.id then group=0;
if first.id or tcount<lag(tcount) then group+1;
run;
然后找到每个组中的最后一个观察值是一件简单的事情。
data want;
set groups;
by id group;
if last.group;
run;