跨id比较数据

2024-04-25 05:15:00 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,如果id中的变量页和步骤相同,我想按id分组。你知道吗

示例: enter image description here

因此,在这里您可以看到id2(原始白色)和id4(绿色)具有完全相同的页面和步骤。你知道吗

示例2-这里您还可以看到我想要的id\u组变量: enter image description here ''

如您所见,groupid\ugroup11和12分配给两个在page和step变量中实际上相同的组。你知道吗

因此,问题是-如果页面和步骤变量的组在id中是相同的,那么如何动态地分配相同的“组”id?你知道吗

这在SAS中可能吗?如果在SAS中不可能。Python可以做到。你知道吗


Tags: id示例steppage步骤动态页面id2
1条回答
网友
1楼 · 发布于 2024-04-25 05:15:00

在SAS中,您可以使用所谓的双道循环(如果您需要更多信息,可以使用googleit)。这将在源表中循环两次,第一次确定哪些ID具有相同的PAGE和STEP值,并设置一个groupid标志。第二次将组ID分配给具有该ID的所有记录

data have;
input page step id;
datalines;
1 1 1
1 2 1
2 3 1
3 4 1
1 1 2
2 2 2
3 3 2
5 1 3
6 2 3
1 1 4
2 2 4
3 3 4
;
run;

data want;
do until(last.id);
set have;
by id;
if first.id then do; /* reset counts when id changes */
    _count=0;
    _same=0;
    end;
_count+1;  /* count number of records per id*/
_same+(page=step); /* count number of times page = step */
if last.id and _count=_same then do;
    _flag+1; /* if count = same then increment _flag by 1 */
    group_id=_flag; 
    end;
drop _: ; /* drop temporary variables */
end;
do until(last.id);
set have;
by id;
output; 
end;
run;

相关问题 更多 >