计算具有多个条件的出现次数

问题描述 投票:2回答:1

我正在尝试计算电子邮件在我的数据中出现的次数(如果它出现在第6周)。我目前正在使用此代码

if week=6 has6=1.
aggregate out=* mode=addvariables overwrite=yes/break=email /has6=max(has6).
aggregate out=* mode=addvariables /break=email /n=sum(has6).

这是按预期工作,但我遗漏了一个我需要的条件。电子邮件地址每周可以多次显示,但我只想每周计算一次。

例如:

  Email            Week         N
[email protected]         6          2
[email protected]       3          
[email protected]       4         
[email protected]    6          1
[email protected]         4          2
[email protected]         4          2

因此即使[email protected]在数据中出现三次,我也只希望她显示为N = 2,因为她在不同的星期(6和4)只出现两次。我不希望它在第4周计算她的第二个实例(这是当前代码正在做的事情:她被计为3,因为她出现了三次)。

所以我的两个条件是:

  1. 仅计算第6周中出现的电子邮件地址至少一次。
  2. 每周只计算一次。

TIA!

spss
1个回答
1
投票

这将重新创建您提供的示例数据:

data list list/email(a50) week(f1).
begin data
"[email protected]" 6
"[email protected]" 3
"[email protected]" 4
"[email protected]" 6
"[email protected]" 4
"[email protected]" 4
end data.

首先,我们会识别每周至少有一次第6周的电子邮件,并标记所有发生的事件:

if week=6 has6=1.
aggregate out=* mode=addvariables overwrite=yes/break=email /has6=max(has6).

现在有两种方法可以继续。

第一种方法:第二次聚合并重新连接结果到数据:

sort cases by email week.
dataset name orig.
dataset declare agg.
aggregate out=agg /break=email week/has6=max(has6).
dataset activate agg.
select if has6.
aggregate out=* mode=addvariables/break email/n=n.
dataset activate orig.
match files /file=* /table=agg /by email week.
exe.

第二种方法:将行与折扣非唯一案例进行比较:

sort cases by email week.
compute countThis=has6.
if $casenum>1 and has6 and lag(email)=email and lag(week)=week countThis=0.
exe.
aggregate out=* mode=addvariables /break=email /n=sum(countThis).
© www.soinside.com 2019 - 2024. All rights reserved.